Created
August 4, 2021 09:59
-
-
Save aviatesk/8ab85df1140843919d418241034f41cf to your computer and use it in GitHub Desktop.
memory effect on TypeScript's type safety
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
interface Foo { | |
bar: undefined | Bar | |
} | |
interface Bar { | |
baz: string | |
} | |
export function getbaz(foo: Foo) { | |
if (foo.bar) { | |
somewrite(foo) | |
return foo.bar.baz // XXX will result in error | |
} | |
return undefined | |
} | |
function somewrite(foo: Foo) { | |
if (Math.random() < 0.5) { | |
foo.bar = undefined | |
} | |
} | |
const foo = {bar: {baz: "hi"}} | |
console.log(getbaz(foo)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment