【发布时间】:2019-12-18 00:55:21
【问题描述】:
给定以下代码:
type Params<F extends (...args: any[]) => any> =
F extends ((...args: infer A) => any)
? A
: never;
const fn00 = (name: string, age: number, single: boolean) => true
type test07 = Params<typeof fn00>
为什么typeof fn00 满足(...args: any[]) => any 的通用约束。
根据文档,typeof 运算符仅返回所检查类型的字符串。基本上:
"string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
typeof 的文档在这里: https://www.typescriptlang.org/docs/handbook/advanced-types.html#typeof-type-guards
【问题讨论】:
-
因为你可以向它传递任何参数并且它返回任何东西。
-
有两个
typeof运算符。有一个 runtimetypeof执行您所说的操作,还有一个编译时typeof计算结果为 TypeScripttype。
标签: typescript