Function和 和有(...args: any[]) => any什么区别?
有趣的是,我刚刚注意到Function不能分配给(...args: any[]) => any,但为什么呢?
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)
Run Code Online (Sandbox Code Playgroud)
显然Function接口在其声明中没有可调用签名。虽然如果你有一个 type 的对象,如果它是type Function,你就可以调用它。有一个(有点旧)未解决的问题microsoft/TypeScript#20007,要求对此进行更改。那里有一条评论说:(...args: any) => any
的初衷
Function是不可调用。换句话说,Functionto 函数类型应该像unknown其他类型一样,但不可调用。我们已经放宽了这个限制,Function通过特殊的大小写在编译器中提供了一个可调用的行为。我们已经讨论过将--noImplicitAny其设为错误,因为调用Functions确实是不安全的。
如果您真的关心这种情况,您可能想要解决该问题,并给出您的并可能描述您的用例。不过,从务实的角度讲,它不太可能改变,所以你最好只注意到奇怪的差异并继续前进。
| 归档时间: |
|
| 查看次数: |
51 次 |
| 最近记录: |