Skip to content

Instantly share code, notes, and snippets.

@yyyyaaa
Created June 28, 2022 05:29
Show Gist options
  • Save yyyyaaa/de6738efc888748cc829335bee1788c5 to your computer and use it in GitHub Desktop.
Save yyyyaaa/de6738efc888748cc829335bee1788c5 to your computer and use it in GitHub Desktop.
Some useful TS types I come across
/**
* This is the generic type useful for declaring a nominal type,
* which does not structurally matches with the base type and
* the other types declared over the same base type
*
* Usage:
* @example
* type Index = Nominal<number, 'Index'>;
* // let i: Index = 42; // this fails to compile
* let i: Index = 42 as Index; // OK
* @example
* type TagName = Nominal<string, 'TagName'>;
*/
type Nominal<T, Name extends string> = T & {
[Symbol.species]: Name
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment