Bug Report
🔎 Search Terms
invariant generic, contravariant inference
Maybe related or perhaps because of the same design limitation mentioned in #44999
🕗 Version & Regression Information
Tested with 4.3.5
⏯ Playground Link
Playground
💻 Code
declareconstd: <T,U>(_: {v: T,m: (t: T)=>U,f: (u: U)=>void})=>voiddeclareconsta: "a"declareconstb: "b"d({v: a,m: t=>t,f: u=>{lettest1: "a"=u// Type 'unknown' is not assignable to type '"a"'.}})d({v: a,m: (t: "a")=>t,f: u=>{lettest2: "a"=u}})d({v: a,m: _=>b,f: u=>{lettest3: "b"=u// Type 'unknown' is not assignable to type '"b"'}})d({v: a,m: ()=>b,f: u=>{lettest4: "b"=u}})🙁 Actual behavior
In 1st and 3rd function call U gets inferred to unknown
🙂 Expected behavior
In 1st function call U should get inferred to "a" and in 3rd function call U should get inferred to "b".
Clearly the contravariant position of U in m seems to be a problem because if we annotate it (as done in 2nd call) or omit it (as done in 4th call) it gets inferred to what is expect and there are no error.
A real world use case:
declareconstbranch:
<T,UextendsT>(_: {test: T,if: (t: T)=>t is U,then: (u: U)=>void})=>voiddeclareconstx: "a"|"b"branch({test: x,if: (t): t is "a"=>t==="a",then: u=>{lettest1: "a"=u// Type '"a" | "b"' is not assignable to type '"a"'}})branch({test: x,if: (t: "a"|"b"): t is "a"=>t==="a",then: u=>{lettest2: "a"=u// compiles}})
Bug Report
🔎 Search Terms
invariant generic, contravariant inference
Maybe related or perhaps because of the same design limitation mentioned in #44999
🕗 Version & Regression Information
Tested with 4.3.5
⏯ Playground Link
Playground
💻 Code
🙁 Actual behavior
In 1st and 3rd function call
Ugets inferred tounknown🙂 Expected behavior
In 1st function call
Ushould get inferred to"a"and in 3rd function callUshould get inferred to"b".Clearly the contravariant position of
Uinmseems to be a problem because if we annotate it (as done in 2nd call) or omit it (as done in 4th call) it gets inferred to what is expect and there are no error.A real world use case: