I wonder if there is an alternative way to specify the function type "any function" that does not use any
(and therefore improves type-safety).
To clarify, I am searching for an alternative for the following type:
type AnyFunctionAsUsual = (...args: any[]) => any;
I was able to replace the return value with unknown
, but there is still an any
in the following definition:
type AnyFunctionReturningUnknown = (...args: any[]) => unknown;
As far as I know, the types AnyFunctionAsUsual
and AnyFunctionReturningUnknown
may take the same values.
And I also think that the latter is sufficiently type-safe (because the any
for the parameters does not disable any type checking, as far as I am concerned).
But eslint
does not like it as it still violates the rule no-explicit-any
, which is included in plugin:@typescript-eslint/recommended
.
Is there a way to define a type that takes the same values as the two types above and also makes the linter happy? (Where disabling the linter or the rule is not the same as making the linter happy.)
You want SomeFunction
to be a top type for functions, where if you have a function f
you can assign it to a variable of type SomeFunction
, and if you have a non-function x
then you can't assign it to SomeFunction
. Sort of the unknown
type for functions. (You can't use unknown
because it accepts non-functions.)
Your (...args: any) => unknown
or (...args: any[]) => unknown
types meet these criteria, but they are not type safe because they use the unsafe any
"type". In a safe type system, there is a tradeoff where values of top types are easy to supply but hard to consume. If I want a value of type unknown
you can give me anything you want. But once I have it, I don't know what to do with it without inspecting it further. If I want a value of type SomeFunction
, you can give me any function you want. So once I have it, I could do anything that works for an arbitrary function, such as inspecting its length
property or... uh... well, there's not much I can do with it. I can't call it, because I don't know what arguments it would accept. So consuming a SomeFunction
will be difficult, or at least tricky. The (...args: any) => unknown
type allows you to call it however you want, even if this would immediately lead to a runtime error:
type SomeFunction = (...args: any) => unknown;
const f: SomeFunction = (x: string) => x.toUpperCase();
f(); // accepted erroneously, runtime error!
f(1, 2, 3); // accepted erroneously, runtime error!
That's the reason why you shouldn't use any
there.
Assuming you want a safe version of SomeFunction
, then we should make sure that the argument list isn't given an "anything-goes" type. Indeed, it should be the opposite. You should be completely unable to call it. That's because function types are contravariant in their parameter types (see Difference between Variance, Covariance, Contravariance and Bivariance in TypeScript and a description of the --strictFunctionTypes
compiler option for more information). In TypeScript, that means the top function type should have a bottom type for its parameter list. And in TypeScript, the bottom type is the never
type:
type SomeFunction = (...args: never) => unknown;
const f: SomeFunction = (x: string) => x.toUpperCase();
f(); // error in TS5.0 and above
f(1, 2, 3); // error
This ability for (...args: never) => unknown
to act as a function top type was implemented mostly in microsoft/TypeScript#35438 and completed in microsoft/TypeScript#52387, which will be released with TypeScript 5.0. Before then it's possible to call f()
above without error (even though it is not safe), but other calls are rejected.