Skip to content

Instantly share code, notes, and snippets.

@devdoomari3
Created August 10, 2018 09:40
Show Gist options
  • Save devdoomari3/9c1cd041ddd863e2dccbfcac367027ed to your computer and use it in GitHub Desktop.
Save devdoomari3/9c1cd041ddd863e2dccbfcac367027ed to your computer and use it in GitHub Desktop.
Typescript force 'atmost once' on type-level
// 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