Typescript 仅从重载中选择特定方法(将传递给 Parameters<T>)
Typescript pick only specific method from overload (to be passed to Parameters<T>)
背景
所以我在从重载的函数中获取特定参数时遇到了问题。例如:
// someLib.d.ts
type Component<T> = {};
type A = {};
type B = {};
type C = {};
type Opts = {};
type ModernOpts = {};
export declare function mount(component: A, options: Opts): Component<A>;
export declare function mount(component: B, options: Opts): Component<B>;
export declare function mount(component: C, options: ModernOpts): Component<C>;
问题是,如果我在另一个文件上这样做:
import { mount } from 'someLib';
type specificMountParams = Parameters<typeof mount>;
我得到的参数是[C, ModernOpts]
,好像没办法得到[A, Opts]
的参数,或者[B, Opts]
.
问题
有没有办法从重载函数中检索特定参数? (所以我可以获得[A, Opts]
参数)
限制和信息
这些类型 (A, B, Opts)
没有被库导出,我需要创建一个需要这种类型的函数来做类似的事情。
来自docs:
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.
C考虑这个例子:
function foo(a: number): number
function foo(a: string): string // last signature
function foo(a: number | string): number | string {
return null as any
}
type Fn = typeof foo
// returns last overloaded signature
type Arguments = Parameters<Fn> // [a: string]
Parameters
始终是 return 函数的最后一个重载签名。
尝试更改顺序:
function foo(a: string): string
function foo(a: number): number// last signature
function foo(a: number | string): number | string {
return null as any
}
type Fn = typeof foo
// returns last overloaded signature
type Arguments = Parameters<Fn> // [a: number]
无法return所有参数的并集,因为它不可靠。
看官方解释here
It's not really possible to make this in a way that's both useful and sound. Consider a function like
declare function fn(n1: number, n2: number): void;
declare function doCall<T extends (a1: any, a2: any) => void>(func: T,
a0: Parameters<T>[0], a1: Parameters<T>[1]): void; ```
If Parameters<T>[0]
returns string | number
, then doCall(fn, 0, "")
would incorrectly succeed. If Parameters<T>[0]>
returns string & number
, then doCall(fn, 0, 0)
would incorrectly fail (and be a big
breaking change). Notably, with conditional types and unions, really
the only kind of functions that can't be typed with a single overload
are exactly the ones that have this failure mode.
目前的行为至少可以正确接受一些呼叫。
您可以在上面的 github 线程中找到一些解决方法
背景
所以我在从重载的函数中获取特定参数时遇到了问题。例如:
// someLib.d.ts
type Component<T> = {};
type A = {};
type B = {};
type C = {};
type Opts = {};
type ModernOpts = {};
export declare function mount(component: A, options: Opts): Component<A>;
export declare function mount(component: B, options: Opts): Component<B>;
export declare function mount(component: C, options: ModernOpts): Component<C>;
问题是,如果我在另一个文件上这样做:
import { mount } from 'someLib';
type specificMountParams = Parameters<typeof mount>;
我得到的参数是[C, ModernOpts]
,好像没办法得到[A, Opts]
的参数,或者[B, Opts]
.
问题
有没有办法从重载函数中检索特定参数? (所以我可以获得[A, Opts]
参数)
限制和信息
这些类型 (A, B, Opts)
没有被库导出,我需要创建一个需要这种类型的函数来做类似的事情。
来自docs:
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.
C考虑这个例子:
function foo(a: number): number
function foo(a: string): string // last signature
function foo(a: number | string): number | string {
return null as any
}
type Fn = typeof foo
// returns last overloaded signature
type Arguments = Parameters<Fn> // [a: string]
Parameters
始终是 return 函数的最后一个重载签名。
尝试更改顺序:
function foo(a: string): string
function foo(a: number): number// last signature
function foo(a: number | string): number | string {
return null as any
}
type Fn = typeof foo
// returns last overloaded signature
type Arguments = Parameters<Fn> // [a: number]
无法return所有参数的并集,因为它不可靠。 看官方解释here
It's not really possible to make this in a way that's both useful and sound. Consider a function like
declare function fn(n1: number, n2: number): void; declare function doCall<T extends (a1: any, a2: any) => void>(func: T, a0: Parameters<T>[0], a1: Parameters<T>[1]): void; ```
If
Parameters<T>[0]
returnsstring | number
, thendoCall(fn, 0, "")
would incorrectly succeed. IfParameters<T>[0]>
returnsstring & number
, thendoCall(fn, 0, 0)
would incorrectly fail (and be a big breaking change). Notably, with conditional types and unions, really the only kind of functions that can't be typed with a single overload are exactly the ones that have this failure mode.
目前的行为至少可以正确接受一些呼叫。
您可以在上面的 github 线程中找到一些解决方法