Skip to content

Instantly share code, notes, and snippets.

@Grohden
Last active October 21, 2021 16:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Grohden/42ec5a0d6508ed4a9986e5bad2100fce to your computer and use it in GitHub Desktop.
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.
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