Skip to content

Instantly share code, notes, and snippets.

@lakinwecker
Created November 14, 2019 14:12
Show Gist options
  • Save lakinwecker/ec7902375cba18c22764576e13815353 to your computer and use it in GitHub Desktop.
Save lakinwecker/ec7902375cba18c22764576e13815353 to your computer and use it in GitHub Desktop.
type A = {
x: string;
};
type B = {
x?: string;
z: string;
};
type C = A | B;
function isA(c: C): c is A {
return (c as A) !== undefined;
}
function complicatedButDoesWork(c: C): string {
if (isA(c)) {
return c.x;
}
if (c.x) {
return c.x;
}
return c.z;
}
function simplerButDoesntWork(c: C): string {
if (c.x) {
return c.x;
}
return c.z;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment