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.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
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 thanany
. For an extra level of protection, one can also useRecord<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.