Skip to content

Generic not inferred from contextual types #25293

Description

TypeScript Version: 2.9.2

Search Terms: generic infer contextual type

Code

constbaz: (n: number)=>number=(// param `n` is correctly inferred to be `number` :-)n=>n);constidentity=<T>(t: T)=>t;constbar: (n: number)=>number=identity(// param `n` is inferred as `any`, expected `number` :-(x=>x);

Expected behavior: TypeScript should infer the generic T in identity from the contextual type.

Actual behavior: TypeScript infers the generic T in identity as any!

Playground Link:

Related Issues:

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions