Skip to content
stoic man

Nominal types

Using nominal types to create type-safe identifiers

Nominal.ts
declare const __nominal__type: unique symbol;
export type Nominal<T, U> = T & {readonly [__nominal__type]: U};
Example.ts
type UserId = Nominal<string, 'UserId'>;
type PostId = Nominal<string, 'PostId'>;
type OrgId = Nominal<string, 'OrgId'>;
type ProjectId = Nominal<string, 'ProjectId'>;
 
type CustomerId = Nominal<string, 'CustomerId'>;
type ClientId = Nominal<string, 'ClientId'>;
 
type projectInvitationToken = Nominal<string, 'projectInvitationToken'>;
type passwordResetToken = Nominal<string, 'passwordResetToken'>;
 
type EUR = Nominal<number, 'EUR'>;
type USD = Nominal<number, 'USD'>;
 
type Miles = Nominal<number, 'Miles'>;
type Kilometers = Nominal<number, 'Kilometers'>;

All snippets