Repository navigation
"...args: infer" on an overloaded function chooses only one function head #26136
Description
Activity
Closing as duplicate of Brian Terlson (@bterlson)'s #26132
- addedDuplicateAn existing issue was already createdAn existing issue was already created
on Aug 2, 2018 Mike North (@mike-north) is this a duplicate, though? #26132 doesn't have overloads, and arguments list inference of
fngives something similar to what you request here:// sample from #26132 type Rec = { foo: (a: string, b: number) => void, bar: (b: boolean, c: RegExp) => void, } type Parameters<T> = T extends (...args: infer U) => any ? U : never; declare var fn: <K extends keyof Rec>(e: K, ...args: Parameters<Rec[K]>) => void; // test parameter extraction type test = Parameters<typeof fn>; // type test = ["foo" | "bar", ...(string | number | boolean | RegExp)[]]
Good catch, I must have read your issue too quickly. Maybe this should be un-tagged as duplicate
Reacted by Brian Terlson- addedSuggestionAn idea for TypeScriptAn idea for TypeScriptIn DiscussionNot yet reached consensusNot yet reached consensusToo ComplexAn issue which adding support for may be too complex for the value it addsAn issue which adding support for may be too complex for the value it addsand removedDuplicateAn existing issue was already createdAn existing issue was already createdIn DiscussionNot yet reached consensusNot yet reached consensus
on Aug 2, 2018 RyanCavanaugh commented
on Aug 7, 2018 MemberMore actionsDiscussed for a while yesterday. This looks reasonably straightforward if you're just capturing the entire parameter list into a single spread, but quickly turns into a bunch of really complicated questions once you do anything else
- What would the behavior of
type Args<F> = F extends (n: number, ...args: infer T) => any ? T : never;be? Would it be sound to just capture the matching overloads, or not? - What happens when you put
Tinto a different argument list? Potentially a combinatorially explosive expansion in to overloads? - More problems I can't recall at the moment
Overall too complex unless there are some important use cases that appear.
Reacted by Mike North- What would the behavior of
Ryan Cavanaugh (@RyanCavanaugh)
some example like
Function.prototype.call
Function.prototype.apply
Function.prototype.bind
TypeScript Version: 3.1.0-dev.20180801
Search Terms: overload, infer, function heads, tuple, rest parameter
Code
Expected behavior:
Ideally, I would hope for the typeof
xto bealthough not entirely correct, I would even take a type wide enough to accomodate both function heads.
Actual behavior:
The type of x ends up being
Playground Link: https://www.typescriptlang.org/play/#src=interface%20Foo%20%7B%0D%0A%20%20(a%3A%20string%2C%20b%3A%20number%2C%20c%3A%20boolean)%3A%20void%3B%0D%0A%20%20(a%3A%20number%2C%20b%3A%20string%2C%20c%3A%20boolean)%3A%20void%3B%0D%0A%7D%0D%0A%0D%0Atype%20Args%3CF%3E%20%3D%20F%20extends%20(...args%3A%20infer%20T)%20%3D%3E%20any%20%3F%20T%20%3A%20never%3B%0D%0A%0D%0Alet%20x%3A%20Args%3CFoo%3E%20%3D%20%5B42%2C%20'hello%20world'%2C%20true%5D%3B%20%2F%2F%20%E2%9C%85%0D%0Ax%20%3D%20%5B'hello%20world'%2C%2042%2C%20true%5D%3B%20%2F%2F%20%F0%9F%92%A5
Related Issues: #24897, #5453 (maybe)