DEV Community

Discussion on: The "any" type in Typescript - simple and short explanation

Collapse
 
somedood profile image
Basti Ortiz

More often than not, the any type is used to denote some form of an object dictionary. For third-party libraries, this may be the configuration object and whatnot.

In those cases, I strongly advocate for the use of the Record<K, V> utility type. It's basically a wrapper over objects with index signatures, but it provides a greater level of type safety than a mere any type.

I can, for example, define a dictionary of string key-value pairs as Record<string, string>, which is definitely safer than any. For an extra level of protection, one can also use Record<string, string|undefined> instead in order to enforce null checks.

The any type should always be a last resort. Ideally, it shouldn't even be an option. In practice? Maybe not so.