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
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
Expected behavior:
Should infer the type of the
nparameter of the second argument asnumber.Actual behavior:
Infers
n: any, which rightly fails whennoImplicitAnyis enabled. However the compiler clearly knows what the type of thenparameter should be... if it's annotated incorrectly, e.g.then we get an error saying
Playground Link