Skip to content

Generic parameter of function is inferred to be {} when argument of conditional type is passed inΒ #25301

Closed
@andriy-kudrya

Description

@andriy-kudrya

TypeScript Version:
2.8.1, 2.9.2, 3.0.0-dev.20180628

Search Terms:
conditional type inference

Code

// very contrived code to demonstrate the issue

// this conditional type trips type inference
type Action<P> = P extends void
    ? { type: string }
    : { type: string, payload: P }

interface Dispatch {
    // this conditional type seems to have no negative impact on type inference
    <P>(action: Action<P>): P extends void ? undefined : P
}

declare const dispatch: Dispatch

const numberAction: Action<number> = {
        type: 'dummy',
        payload: 0,
    }
    // infered result type is {}, expected one is number
    , numberResult: number = dispatch(numberAction)

const voidAction: Action<void> = {
        type: 'dummy',
    }
    // seems infered P is {}, so it is not allowed to pass voidAction to dispatch
    , voidResult: undefined = dispatch(voidAction)

Expected behavior:
Type checking succeedes

Actual behavior:
Type checking fails for two places:

  • numberResult: number = dispatch(numberAction)
  • voidResult: undefined = dispatch(voidAction)

Playground Link:
Playground

I believe this sample captures the issue but just in case there is a more verbose sample with original types

Related Issues:
Not sure #25299 #24784

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