You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The type isn't captured when using generics inside of a configuration object in some cases.
In particular, the function functionAThatTakesAnObject should capture the type of aNumber so that the type of shouldBeInferedToBeANumber should be number but instead it's given type {}
Code
functionfunctionA<T>(funcThatReturnsT: (something: any)=>T,funcThatTakeT: (t: T)=>any){letsomething=undefinedasany;lethasTypeT=funcThatReturnsT(something);returnfuncThatTakeT(hasTypeT);}letaNumber=5;// worksfunctionA(()=>aNumber,shouldBeInferedToBeANumber=>shouldBeInferedToBeANumber.toExponential());// still worksfunctionA(_=>aNumber,shouldBeInferedToBeANumber=>shouldBeInferedToBeANumber.toExponential());/** * this is the same as `functionA` but it takes a configuraton object instead */functionfunctionAThatTakesAnObject<T>(opts: {funcThatReturnsT: (something: any)=>T,funcThatTakeT: (t: T)=>any}){letsomething=undefinedasany;lethasTypeT=opts.funcThatReturnsT(something);returnopts.funcThatTakeT(hasTypeT);}// worksfunctionAThatTakesAnObject({funcThatReturnsT: ()=>aNumber,// if there is no parameter, it worksfuncThatTakeT: shouldBeInferedToBeANumber=>shouldBeInferedToBeANumber.toExponential()});// DOES NOT WORKfunctionAThatTakesAnObject({funcThatReturnsT: _=>aNumber,// if there is a parameter, it breaksfuncThatTakeT: shouldBeInferedToBeANumber=>shouldBeInferedToBeANumber.toExponential()});
Expected behavior:
shouldBeInferedToBeANumber should be inferred to be of type `number
Actual behavior:
shouldBeInferedToBeANumber is given type {}
The text was updated successfully, but these errors were encountered:
ricokahler
changed the title
[bug] type inference in generic functions breaks
[bug] type inference in generic functions breaks with configuration object
May 11, 2017
ricokahler
changed the title
[bug] type inference in generic functions breaks with configuration object
[bug?] type inference in generic functions breaks with configuration object
May 11, 2017
ricokahler
changed the title
[bug?] type inference in generic functions breaks with configuration object
Type not captured in generic functions when using a configuration object
May 21, 2017
I asked about this on stackoverflow and someone discovered that when r...
// this works!functionAThatTakesAnObject({funcThatReturnsT: (r: any)=>aNumber,// if there is a parameter, it breaksfuncThatTakeT: shouldBeInferedToBeANumber=>shouldBeInferedToBeANumber.toExponential()});
...in the call functionAThatTakesAnObject has it's type explicitly stated, then there is no error. I find this strange because r already had the type of any but it works when I re-assert that is has type of any.
TypeScript Version: 2.3.2
The type isn't captured when using generics inside of a configuration object in some cases.
In particular, the function
functionAThatTakesAnObject
should capture the type ofaNumber
so that the type ofshouldBeInferedToBeANumber
should benumber
but instead it's given type{}
Code
Expected behavior:
shouldBeInferedToBeANumber
should be inferred to be of type `numberActual behavior:
shouldBeInferedToBeANumber
is given type{}
The text was updated successfully, but these errors were encountered: