Last active
October 21, 2021 16:50
-
-
Save Grohden/42ec5a0d6508ed4a9986e5bad2100fce to your computer and use it in GitHub Desktop.
Collection of typescript type system issues or bugs that don't get caught by it.
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 Foo = { | |
// Assigning undefined here is wrong. | |
// this value either HAVE A KEY WITH VALUE or DON'T HAVE A KEY | |
[key: string]: { foo: string } | |
// const foo: Foo = { key: undefined } | |
// 'key' in foo // true, but should never be the case, because if there's a key, there's a value. | |
} | |
// note: the only case where the index signature might be correctly represented | |
// is when we use a proxy, that way we can guarantee that we always return something | |
// for any index access | |
const fooRecord: Foo = { | |
// Hey, why ts is not giving me an error here? why is it not requiring me | |
// to implement all the infinite sequences on this dict? | |
"foo": { foo: '' } | |
} | |
function bar(foo: Foo) { | |
// foo["bar"] is recognized as never returning undefined, so the || is basically dead code for ts type system | |
// but we receive no warning about that.. this is actually very dangerous | |
const wrongType = foo["bar"] || 121; | |
wrongType.foo.trim() // error :DDDDDDD | |
} | |
bar(fooRecord) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment