Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save charlypoly/81ccf51dea0e063dbc4775a7bd1ba605 to your computer and use it in GitHub Desktop.
Save charlypoly/81ccf51dea0e063dbc4775a7bd1ba605 to your computer and use it in GitHub Desktop.
TypeScript equality narrowing
function example(x: string | number, y: string | boolean) {
if (x === y) {
x
// `x` is of type `string`
y
// `y` is of type `string`
} else {
x
// `x` is of type `string | number`
y
// `y` is of type `string | boolean`
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment