![](/img/trans.png)
[英]No constituent of type 'true | CallableFunction' is callable
[英]TypeScript: Define a callable type without CallableFunction properties
让我们声明一个具有调用签名的类型T
:
type T = () => number;
让我们也声明一个这种类型的变量t
:
declare const t: T;
在 TypeScript Playground 或 IDE 中,我们立即看到t
是T
类型,它有很多我们从未明确声明的方法和属性: apply
、 bind
、 caller
等等。
我们如何声明T
使其仅由其调用签名定义? (TS 3.8.3+)
值得注意的是,类型Omit<T, "apply" | "bind" | "caller">
Omit<T, "apply" | "bind" | "caller">
Omit<T, "apply" | "bind" | "caller">
是{}
, Omit<T, "">
。 此外,根据tsc
, Omit<CallableFunction, "">
不是可调用类型。
对于核心类型(字符串、数字和 yes 函数),您不能从类型声明中做到这一点。
与编译--noLib
,包括您自己的定义someFile.d.ts
的interface String
, interface Function
等。
定义仍将全局应用于所有string
/ number
/ Function
等。例如,如果您不包含.bind
,则任何函数都不会出现它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.