7 ms·
Honestly every time I worked on TypeScript codebase and the type definition "any" started popping out more and more often, I felt I'm starring into the abyss.
by undebuggable 2y ago
Honestly every time I worked on TypeScript codebase and the type definition "any" started popping out more and more often, I felt I'm starring into the abyss.
- epolanski 2y agoThere are valid use cases for any (such as type constraints). Playground with example here: https://tinyurl.com/5ahs366a https://tinyurl.com/5ahs366a
- undebuggable 2y agoThat was the tipping point in transition from "we are serious and use static typing instead of lame JavaScript" into "ok we lost control over this thing".
- epolanski 2y agoWhat do you mean? This is perfectly sound and valid from both a practical and theoretical pov. Which is why I was pointing out that there are scenarios like constraints where any not only makes sense but is the correct type.
- tyilo 2y agoSeems to also work without any: type Values<T extends Record<string | number | symbol, unknown>> = T[keyof T]
- epolanski 2y agoSure, there's many other ways to type it, but none adds any kind of additional safety or strictness over Record<any, any> which was my point that `any` is the correct type in many cases, except when it widens a type. But in my case it's not widening anything, in Record<A, B>, B can already be `any`thing. People tend to see it as an unsafe escape hatch (which is how it is abused), but it's just a set of all possible types.
- matt_kantor 2y ago> it's just a set of all possible types `unknown` is the set of all possible types (it's the top type[0]). `any` goes beyond that—it basically turns off the type checker[1][2]. [0]: https://en.wikipedia.org/wiki/Top_type https://en.wikipedia.org/wiki/Top_type [1]: https://tsplay.dev/mA9vXm https://tsplay.dev/mA9vXm [2]: https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#any https://www.typescriptlang.org/docs/handbook/2/everyday-type...
- epolanski 2y agoYour 1 isn't equivalent to my example, line 11 is not constraining the type.
- matt_kantor 2y agoI know it's not equivalent, it was just an example to show what `any` does (and that it's more than "just a set of all possible types"). The `T extends Record<any, any>` on line 11 is a type parameter constraint though. Are you referring to something else when you say "constraining the type"?
- epolanski 2y agoIt works differently based on where those anys are and what that Record<any, any> refers to due to type variance.
- undebuggable 2y agoIt's a "null" and "undefined" discussion all over, but now with transpiler.
- remolueoend 2y ago'any' has always been intended as an escape hatch, so no abuse here [1]. The type representing the set of all possible values (the top type in the type lattice) is 'unknown'. [1] https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#any https://www.typescriptlang.org/docs/handbook/2/everyday-type... [2] https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-0.html#new-unknown-top-type https://www.typescriptlang.org/docs/handbook/release-notes/t...
- pcthrowaway 2y agoNote, the type `PropertyKey` is shorthand for `string | number | symbol` in Typescript
- chris_pie 2y agoIn your example, I'd use Record<PropertyKey, unknown> instead
- EndShell 2y agoPeople generally just give up after a while with the type system. I consider myself decent with TypeScript and you end up spending a good chuck of your time satisfying the compiler for something which is literally a one liner in plain JavaScript.