[英]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.