Skip to content

Instantly share code, notes, and snippets.

@faustienf
Created March 22, 2022 13:07
Show Gist options
  • Save faustienf/c4ea9f7a7f2cfafd537f94a3bd059147 to your computer and use it in GitHub Desktop.
Save faustienf/c4ea9f7a7f2cfafd537f94a3bd059147 to your computer and use it in GitHub Desktop.
Opaque type in TypeScript
declare const opaque: unique symbol;
type Opaque<T, OpaqueType> = T & { readonly [opaque]: OpaqueType };
// examples
type Timestamp = Opaque<number, 'Timestamp'>;
const timestamp: Timestamp = Math.random() // Type 'number' is not assignable to type 'Timestamp'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment