-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Generic type infer in overloaded function #32418
Comments
Generic inference isn't able to do overload resolution at the same time |
Just to add some notes. function foo (arg: string): string
function foo (arg1: number, arg2: number): number
function foo (...args: any[]) {
// do something
}
type Param = Parameters<foo>
// expected: [string] | [number, number]
// actual: number
type Result = ReturnType<foo>
// expected: string | number
// actual: number |
@shigma This works most of the time: function foo (arg: boolean): string
function foo (arg: string): string
function foo (arg1: number, arg2: number): number
function foo (...args: any[]): any {
// do something
}
type AllParameters<T> =
T extends { (...a: infer A1): any; (...a: infer A2): any; (...a: infer A3): any; (...a: infer A4): any; } ? A1 | A2 | A3 | A4 :
T extends { (...a: infer A1): any; (...a: infer A2): any; (...a: infer A3): any; } ? A1 | A2 | A3:
T extends { (...a: infer A1): any; (...a: infer A2): any; } ? A1 | A2:
T extends { (...a: infer A1): any } ? A1 :
never
type Param = AllParameters<typeof foo> I say most of the time because if you have a no parameter overload @RyanCavanaugh Is this something that can be recommended? Or is it in the recursive type alias category, the compiler allows it but better not. |
@RyanCavanaugh the playground codes in my first comment works fine in |
Why doesn't generic inference do overloading resolution at the same time? |
TypeScript Version: 3.5.2
Code
Expected behavior:
usePromise has no type issues without generic type annotation.
Actual behavior:
usePromise has issues without generic type annotation.
Playground Link:
http://www.typescriptlang.org/play/#code/KYDwDg9gTgLgBDAnmYcCSA7MBXGAxAQwGMZpEAeAZRgJmABp0tcBnOAXjmwwBNgAzAJYZgPAHwcmOGG1B1ebbnyEieAKDhwA-HAAULGnQAkALjgAFKBAC2glsCqHgYgJQcJlm3YfVazjXBmusLSLKYWVrb25JihYowGfuGeUT5Oru4RXtG+dGJqaqCQsHD83CSCEBhc9inejn5iwcz4xKRQiGaxuIQkZA15Lma5qAA+cBjYADZTheDQ8GUYFVU1wHU56c3Sve2dUj1t-SPxcMKCMIIEUyPDTkNwI3PFi+WXq9i1kfUjjN0yTQCIUOfQ6XRau2OTj+LRY8SBGAuVxuTjufnoCNC4NCGIeTzUn3W33sujU21YRkYAH03Ox8ppNMCZEYAHQwAAWwAwul0AG0AG4AXVpEn5LgxDLgAAYJQzeQBGKVKwW4gqEjYOSbWABGwCgjF5Wt1UEFgPJzOpIoCjNhrI5XJ5AuFmTFss0MutcAVSqlKrULiAA
Related Issues:
No
The text was updated successfully, but these errors were encountered: