-
Notifications
You must be signed in to change notification settings - Fork 12.8k
"...args: infer" on an overloaded function chooses only one function head #26136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
@mike-north is this a duplicate, though? #26132 doesn't have overloads, and arguments list inference of // 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 |
Discussed 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
Overall too complex unless there are some important use cases that appear. |
@RyanCavanaugh |
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
x
to 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)
The text was updated successfully, but these errors were encountered: