Closed
Description
I would like to enforce using unknown
instead of any
whenever possible for stricter typing.
Fail
function foo(x: any) {}
function foo(): any {}
Pass
function foo(x: unknown) {}
function foo(): unknown {}
Exceptions
There are a couple of exceptions:
- For function arguments and assignment,
{[key: string]: any}
cannot be enforced into{[key: string]: unknown}
, because it has special behavior:The index signature
{[key: string]: any}
in TypeScript behaves specially: it’s a valid assignment target for any object type. This is a special rule, since types with index signatures don’t normally produce this behavior. - For
new(...args: any[])
, I remember it not working when I tried to useunknown
there.
There might be more exceptions I haven't thought of.
Because of the above exceptions, I cannot use the @typescript-eslint/ban-types
rule for this.