Hacker News new | past | comments | ask | show | jobs | submit login

There are valid use cases for any (such as type constraints).

Playground with example here: https://tinyurl.com/5ahs366a




Seems to also work without any:

    type Values<T extends Record<string | number | symbol, unknown>> = T[keyof T]


Sure, 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.


> 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...


It's a "null" and "undefined" discussion all over, but now with transpiler.


Your 1 isn't equivalent to my example, line 11 is not constraining the type.


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"?


It works differently based on where those anys are and what that Record<any, any> refers to due to type variance.


'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...


unknown is not equivalent to any as a type constraint: doesn't work.

https://www.totaltypescript.com/any-considered-harmful#type-...


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].

[0]: https://tsplay.dev/Wy0Ogm

[1]: https://github.com/microsoft/TypeScript/issues/55667


Note, the type `PropertyKey` is shorthand for `string | number | symbol` in Typescript


That 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".


What 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.


In your example, I'd use Record<PropertyKey, unknown> instead




Consider applying for YC's Summer 2025 batch! Applications are open till May 13

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: