Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ELLIOTTCABLE/1c37fc8161861ffd81915f97acfd71c9 to your computer and use it in GitHub Desktop.
Save ELLIOTTCABLE/1c37fc8161861ffd81915f97acfd71c9 to your computer and use it in GitHub Desktop.
class Checkpoint<Semantic> {
type: 'Script' | 'Statement'
constructor(blah: any, type: 'Script'): Checkpoint<Script>
constructor(blah: any, type: 'Statement'): Checkpoint<Statement>
constructor(blah: any, type: 'Script' | 'Statement') {
// ...
this.type = type
}
producesScript(): this is Checkpoint<Script> {
return this.type === 'Script'
}
producesStatement(): this is Checkpoint<Script> {
return this.type === 'Statement'
}
f<T>(callback: (val: Semantic) => T): T {
// ... now do runtime type-checks, and narrow knowledge of the resultant type from `callback`
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment