Created
March 22, 2022 13:07
-
-
Save faustienf/c4ea9f7a7f2cfafd537f94a3bd059147 to your computer and use it in GitHub Desktop.
Opaque type in TypeScript
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
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