Created
August 10, 2018 09:40
-
-
Save devdoomari3/9c1cd041ddd863e2dccbfcac367027ed to your computer and use it in GitHub Desktop.
Typescript force 'atmost once' on type-level
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
// use cases: class decorators, etc | |
function forceOnlyOnce (test: string & {__T?: undefined}) { | |
const test2: string = test | |
return test2 as string & { __T: null } | |
} | |
const a = forceOnlyOnce('123') | |
// type-error below: | |
forceOnlyOnce(a) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment