Skip to content

Generic type infer in overloaded function #32418

Closed
@Brooooooklyn

Description

@Brooooooklyn

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions