{"library":"newtype-ts","title":"TypeScript Newtypes","description":"newtype-ts provides a robust and performant implementation of newtypes in TypeScript, allowing developers to define distinct types that share the same underlying runtime representation. This helps enforce type safety at compile time, preventing logical errors such as accidentally assigning a `USD` value to a `EUR` variable. The library is currently at version 0.3.5 and is actively maintained, with recent releases focusing on polish and bug fixes. Its key differentiators include a strong reliance on `fp-ts` and `monocle-ts` for functional programming patterns and optics, ensuring no runtime overhead, and offering built-in refinements for common data types like `Integer` or `NonEmptyString`. It supports TypeScript 3.5.1+ and is designed for performance, with newtype operations showing negligible overhead compared to raw type operations.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install newtype-ts"],"cli":null},"imports":["import { Newtype, iso } from 'newtype-ts'","import { iso } from 'newtype-ts'","import { prism } from 'newtype-ts'","import { NonZero, prismNonZero } from 'newtype-ts/lib/NonZero'"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { Newtype, iso } from 'newtype-ts';\n\ninterface Email extends Newtype<{ readonly Email: unique symbol }, string> {}\n\n// Create an Iso for Email, allowing wrapping and unwrapping\nconst isoEmail = iso<Email>();\n\n// Example: a function that strictly requires an Email newtype\ndeclare function sendEmail(to: Email, subject: string, body: string): Promise<boolean>;\n\n// Wrap a string into an Email newtype\nconst userEmail: Email = isoEmail.wrap('test@example.com');\n\n// Use the newtype in a type-safe context\nsendEmail(userEmail, 'Hello', 'This is a test email.').then(success => {\n  if (success) {\n    console.log('Email sent successfully!');\n  } else {\n    console.error('Failed to send email.');\n  }\n});\n\n// This would cause a static type error:\n// sendEmail('wrong@example.com', 'Subject', 'Body');\n\n// Unwrap the email for operations requiring the base type\nconst emailString: string = isoEmail.unwrap(userEmail);\nconsole.log(`Unwrapped email: ${emailString}`);\n","lang":"typescript","description":"This quickstart demonstrates how to define a custom newtype (`Email`) using `newtype-ts`, wrap a base type into it, and then use it in a type-safe function, preventing direct usage of the underlying primitive type.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}