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
Search Terms: infer, inferred, declaration, generic function
Code
// Things are objects that store a functionclassThing<In,Out>{f: (x: In)=>Out;privateconstructor(f: (x: In)=>Out){this.f=f;}// Creates an identical thingpublicanother(): Thing<In,Out>{returnnewThing<In,Out>(this.f);}// Creates a thing with the given functionpublicstaticcreate<In,Out>(f: (x: In)=>Out): Thing<In,Out>{returnnewThing<In,Out>(f);}}// Type inference works here, understands that x => x is string => stringconsttest: Thing<string,string>=Thing.create(x=>x);// Type inference does not work here, even though .another() returns something with the same type as the original thingconsttest2: Thing<string,string>=Thing.create(x=>x).another();// Type inference works here because we helped it along by defining x: string, but it's redundant.consttest3: Thing<string,string>=Thing.create((x: string)=>x).another();
Expected behavior: test, test2, and test3 would all be inferred properly.
Actual behavior: test2 produces an error: "Type 'unknown' is not assignable to type 'string'."
The contextual type of an expression can supply inference candidates via the return type, but this process is not recursive and it's very tenuous to apply that logic any further than it already goes.
#30134 would be a prerequisite to handling this in all cases.
Search Terms: infer, inferred, declaration, generic function
Code
Expected behavior: test, test2, and test3 would all be inferred properly.
Actual behavior: test2 produces an error: "Type 'unknown' is not assignable to type 'string'."
Playground Link: Playground Link
Related Issues: (I couldn't find any)
The text was updated successfully, but these errors were encountered: