Skip to content

Failure to infer parameter type #25444

@pelotom

Description

@pelotom

TypeScript Version: 3.0.0-dev.20180704

Search Terms: parameter inference

For context, I'm trying to provide accurate typings for a JS library (Material UI). What follows is an extreme simplification of the problem, dealing with functions rather than components:

Code

const foo = <A>(f: (x: A) => void, x: A) => f(x);

foo(
  (x: (n: number) => void) => x(42),
  n => console.log(n)
);

Expected behavior:

Should infer the type of the n parameter of the second argument as number.

Actual behavior:

Infers n: any, which rightly fails when noImplicitAny is enabled. However the compiler clearly knows what the type of the n parameter should be... if it's annotated incorrectly, e.g.

(n: string) => console.log(n)

then we get an error saying

Argument of type '(x: (n: number) => void) => void' is not assignable to parameter of type '(x: (n: string) => void) => void'.
  Types of parameters 'x' and 'x' are incompatible.
    Types of parameters 'n' and 'n' are incompatible.
      Type 'number' is not assignable to type 'string'.

Playground Link

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