Created
June 28, 2022 05:29
-
-
Save yyyyaaa/de6738efc888748cc829335bee1788c5 to your computer and use it in GitHub Desktop.
Some useful TS types I come across
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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