TypeScript Newtypes

0.3.5 · active · verified Sun Apr 19

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.

Common errors

Warnings

Install

Imports

Quickstart

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.

import { Newtype, iso } from 'newtype-ts';

interface Email extends Newtype<{ readonly Email: unique symbol }, string> {}

// Create an Iso for Email, allowing wrapping and unwrapping
const isoEmail = iso<Email>();

// Example: a function that strictly requires an Email newtype
declare function sendEmail(to: Email, subject: string, body: string): Promise<boolean>;

// Wrap a string into an Email newtype
const userEmail: Email = isoEmail.wrap('test@example.com');

// Use the newtype in a type-safe context
sendEmail(userEmail, 'Hello', 'This is a test email.').then(success => {
  if (success) {
    console.log('Email sent successfully!');
  } else {
    console.error('Failed to send email.');
  }
});

// This would cause a static type error:
// sendEmail('wrong@example.com', 'Subject', 'Body');

// Unwrap the email for operations requiring the base type
const emailString: string = isoEmail.unwrap(userEmail);
console.log(`Unwrapped email: ${emailString}`);

view raw JSON →