Created
November 14, 2019 14:12
-
-
Save lakinwecker/ec7902375cba18c22764576e13815353 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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