Skip to content
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

Closed
Brooooooklyn opened this issue Jul 16, 2019 · 5 comments
Closed

Generic type infer in overloaded function #32418

Brooooooklyn opened this issue Jul 16, 2019 · 5 comments
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@Brooooooklyn
Copy link

TypeScript Version: 3.5.2

Code

export type InputFactory<State, Inputs = undefined> = Inputs extends undefined
  ? (state$: Promise<State>) => Promise<State>
  : (inputs$: Promise<Inputs>, state$: Promise<State>) => Promise<State>

export function usePromise<State>(inputFactory: InputFactory<State>): State | null
export function usePromise<State>(inputFactory: InputFactory<State>, initialState: State): State
export function usePromise<State, Inputs>(
  inputFactory: InputFactory<State, Inputs>,
  initialState: State,
  inputs: Inputs,
): State

usePromise(
(inputs$, _) =>
    inputs$.then(([v]) => v), // why typescript want Promise<number[]> here???? I hovered my mouse to userPromise and I could see the generic params in usePromise was inferred to <0, [number]>. So I assumed the typechecker would like Promise<0> or Promise<number> instead of Promise<number[]> here
    0,
    [1000],
)

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

@RyanCavanaugh RyanCavanaugh added the Design Limitation Constraints of the existing architecture prevent this from being fixed label Jul 31, 2019
@RyanCavanaugh
Copy link
Member

Generic inference isn't able to do overload resolution at the same time

@shigma
Copy link

shigma commented Aug 4, 2019

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

@dragomirtitian
Copy link
Contributor

dragomirtitian commented Aug 4, 2019

@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>

Playground

I say most of the time because if you have a no parameter overload unknown[] creeps in there and there is at least one issue that shows how this behaves strangely in some cases.

@RyanCavanaugh Is this something that can be recommended? Or is it in the recursive type alias category, the compiler allows it but better not.

@Brooooooklyn
Copy link
Author

@RyanCavanaugh the playground codes in my first comment works fine in [email protected]

@Nokel81
Copy link

Nokel81 commented Feb 3, 2021

Why doesn't generic inference do overloading resolution at the same time?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed
Projects
None yet
Development

No branches or pull requests

5 participants