What is the difference between Function and (...args: any[]) => any?
Interestingly, I just noticed that Function is not assignable to (...args: any[]) => any, but why?
declare let foo: Function;
declare let bar: (...args: any[]) => any;
bar = foo;
// ~~~
// ^^^
// Type 'Function' is not assignable to type '(...args: any[]) => any'.
// Type 'Function' provides no match for the signature '(...args: any[]): any'. (2322)
Playground
Apparently the Function interface doesn't have a callable signature in its declaration. Although if you have an object of type Function, you're allowed to call it if it were of type (...args: any) => any. There is a (somewhat old) open issue, microsoft/TypeScript#20007, asking for this to be changed. There's a comment there which says:
The original intention of
Functionis to not be callable. in other words,Functionto function types should be likeunknownto other types, but not callable. we have since relaxed this restriction givingFunctiona callable behavior in the compiler through special casing. We have talked about making this a--noImplicitAnyerror since, it is really unsafe to callFunctions.
If you really care about this situation, you might want to go to that issue and give it your 👍 and possibly describe your use case. Pragmatically speaking, though, it's unlikely to be changed, so you're probably better off just noting the weird discrepancy and moving on.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With