TS函数重载类型推断

当从函数重载推断类型时,只会从最后一个函数签名中推断,比如:

typescript
interface EventBus {
  (
event
: 'refresh',
type
: string):void;
(
event
: 'query',
data
: number):void;
} type
Inferred
=
Parameters
<EventBus> extends [infer
K
, ...infer
U
] ?
U
: never;
// type Inferred === [number]

当从多个调用签名中推断类型时(比如函数重载类型),总是会从最后一个签名中推断(因为是最有可能包含所有情况的签名)。这里没有办法展示出重载的参数类型数组。

When inferring from a type with multiple call signatures (such as the type of an overloaded function), inferences are made from the last signature (which, presumably, is the most permissive catch-all case). It is not possible to perform overload resolution based on a list of argument types.

在GitHub上编辑
上次更新于:
评论加载中 (ง •̀ω•́)ง