Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save charlypoly/9d767ae22098793be76efea6bb9b4827 to your computer and use it in GitHub Desktop.
Save charlypoly/9d767ae22098793be76efea6bb9b4827 to your computer and use it in GitHub Desktop.
TypeScript Control Flow Analysis indirections support
function doSomething(x: string | number | boolean) {
const isString = typeof x === "string"
const isNumber = typeof x === "number"
const isStringOrNumber = isString || isNumber
if (isStringOrNumber) {
x
// `x` is of type `string | number`
if (typeof x === "number" && x > 0) {
x
// `x` is of type `number`
}
} else {
x
// `x` is of type `boolean`
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment