Skip to content

Generic not inferred from contextual types #25293

Closed
@OliverJAsh

Description

@OliverJAsh

TypeScript Version: 2.9.2

Search Terms: generic infer contextual type

Code

const baz: (n: number) => number = (
    // param `n` is correctly inferred to be `number` :-)
    n => n
);

const identity = <T>(t: T) => t;

const bar: (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