Re:TypeScript types can run DOOM [video]
by matt_kantor on Wednesday February 26, 2025 @05:17PM
(#43188889)
> 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
[1]: https://tsplay.dev/mA9vXm
[2]: https://www.typescriptlang.org/docs/handbook/2/everyday-type...
Re:TypeScript types can run DOOM [video]
by epolanski on Thursday February 27, 2025 @02:35AM
(#43192178)
Your 1 isn't equivalent to my example, line 11 is not constraining the type.
Re:TypeScript types can run DOOM [video]
by matt_kantor on Thursday February 27, 2025 @07:05AM
(#43193582)
I 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"?
Re:TypeScript types can run DOOM [video]
by epolanski on Thursday February 27, 2025 @12:38PM
(#43196564)
It works differently based on where those anys are and what that Record<any, any> refers to due to type variance.
Re:TypeScript types can run DOOM [video]
by undebuggable on Thursday February 27, 2025 @05:26AM
(#43193024)
It's a "null" and "undefined" discussion all over, but now with transpiler.
Re:TypeScript types can run DOOM [video]
by remolueoend on Wednesday February 26, 2025 @05:18PM
(#43188905)
'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-type...
[2] https://www.typescriptlang.org/docs/handbook/release-notes/t...
Re:TypeScript types can run DOOM [video]
by epolanski on Thursday February 27, 2025 @02:40AM
(#43192207)
unknown is not equivalent to any as a type constraint: doesn't work.
https://www.totaltypescript.com/any-considered-harmful#type-...
Re:TypeScript types can run DOOM [video]
by matt_kantor on Thursday February 27, 2025 @07:27AM
(#43193748)
The widest possible function type is `(...args: never) => unknown`. This is because parameters are contravariant, and `never` is the bottom type. Using that type works in the author's example[0].
I've got an issue open about TypeScript's provided `ReturnType` type which is somewhat related to this[1].
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.