重载函数的参数泛型不包含所有选项
给定一个重载函数example
.
function example(a: string): number
function example(a: string, b?: string): number {
return 1
}
type Result = Parameters<typeof example>
我希望 Result 包含 example
参数的所有选项,而不仅仅是第一个/最上面的参数集.如何获取参数?
I'd expect Result to contain ALL options for arguments of example
, not just the first / top-most argument set. How can I get the parameters?
在对的回答中问题重复 @ford04在此处的回答中提到的限制,仅infer
查看最后一个重载签名,确认.
In the answers to the question this duplicates the limitation mentioned in @ford04's answer here, that infer
only looks at the last overloaded signature, is acknowledged.
但是这个答案表明这并非完全不可能;您可以梳理出一些有关重载的信息,至少对于具有任意固定数量的函数.但它毛茸茸的和丑陋的,其中可能存在错误,请参阅 microsoft/TypeScript#28867.这是一种方法:
But this answer shows it's not completely impossible; you can tease out some information about overloads, at least for functions with up to some arbitrary fixed number of them. But it's hairy and ugly and there might be bugs in it, see microsoft/TypeScript#28867. Here's one way of doing it:
type Overloads<T> =
T extends {
(...args: infer A1): infer R1;
(...args: infer A2): infer R2;
(...args: infer A3): infer R3;
(...args: infer A4): infer R4
} ? [
(...args: A1) => R1,
(...args: A2) => R2,
(...args: A3) => R3,
(...args: A4) => R4
] : T extends {
(...args: infer A1): infer R1;
(...args: infer A2): infer R2;
(...args: infer A3): infer R3
} ? [
(...args: A1) => R1,
(...args: A2) => R2,
(...args: A3) => R3
] : T extends {
(...args: infer A1): infer R1;
(...args: infer A2): infer R2
} ? [
(...args: A1) => R1,
(...args: A2) => R2
] : T extends {
(...args: infer A1): infer R1
} ? [
(...args: A1) => R1
] : any
type OverloadedParameters<T> =
Overloads<T> extends infer O ?
{ [K in keyof O]: Parameters<Extract<O[K], (...args: any) => any>> } : never
type OverloadedReturnType<T> =
Overloads<T> extends infer O ?
{ [K in keyof O]: ReturnType<Extract<O[K], (...args: any) => any>> } : never
Overloads
类型别名采用函数类型 T
并返回其调用签名的元组(最多四个重载).并且 OverloadedParameters
和 OverloadedReturnType
映射 Parameters
和 ReturnType
元组,分别.
The Overloads<T>
type alias takes a function type T
and returns a tuple of its call signatures (for up to four overloads). And OverloadedParameters<T>
and OverloadedReturnType<T>
map Parameters<T>
and ReturnType<T>
over that tuple, respectively.
让我们看看它的实际效果(在纠正您的示例以使其实际上具有多个重载之后,如另一个答案中所做的那样):
Let's see it in action (after correcting your example so that it actually has multiple overloads, as done in the other answer):
function example(a: string): number
function example(a: string, b: string): number
function example(a: string, b?: string): number {
return 1
}
type ExampleOverloads = Overloads<typeof example>
// type ExampleOverloads = [(a: string) => number, (a: string, b: string) => number]
type ExampleParameters = OverloadedParameters<typeof example>
// type ExampleParameters = [[string], [string, string]]
对我来说看起来很合理.好的,希望有帮助;祝你好运!
Looks reasonable to me. Okay, hope that helps; good luck!