Closed
Description
TypeScript Version: 2.9
Search Terms:
function parameter inference
Code
interface MyInterface<T> {
retrieveGeneric: (parameter: string) => T,
operateWithGeneric: (generic: T) => string
}
const inferTypeFn = <T>(generic: MyInterface<T>) => generic;
// inferred type for myGeneric = MyInterface<{}>, `generic.toFixed()` marked as error (as {} doesn't have .toFixed())
const myGeneric = inferTypeFn({
retrieveGeneric: parameter => 5,
operateWithGeneric: generic => generic.toFixed()
});
// inferred type for myGeneric = MyInterface<number>, everything OK
const myWorkingGeneric = inferTypeFn({
retrieveGeneric: (parameter: string) => 5,
operateWithGeneric: generic => generic.toFixed()
});
Expected behavior:
myGeneric
has every type correctly inferred, parameter
is a string, generic
is a number.
Actual behavior:
it doesn't infer the correct type for generic
parameter unless you manually specify the type of parameter
(which it already had the right type)