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
functionid<T>(x: T): T{returnx}functioncomposition<A,B,C>(f: (p1: A)=>B,g: (p2: B)=>C,): (p: A)=>C{return(x: A)=>g(f(x))}consta=(x: number)=>x+1constcomp=composition(a,id)// Error// Argument of type '<T>(x: T) => T' is not assignable to parameter of type '(p1: unknown) => number'constcomp2=composition(id,a)
Expected behavior:
No errors shows.
The type of function comp2 should be (p: number) => number.
Actual behavior:
Type of function comp is (p: number) => number which is correct.
But type of function comp2 is (p: never) => number which is not correct.
Also TypeScript shows the error: Argument of type '<T>(x: T) => T' is not assignable to parameter of type '(p1: unknown) => number'
This is effectively a duplicate of #30727. As I comment here, it's not immediately clear how we could do better given the left-to-right nature of our type inference algorithm. It's one of those examples that require full unification to make the correct inferences.
TypeScript Version: 3.9.2
Search Terms: two generic functions composition
Code
Expected behavior:
No errors shows.
The type of function
comp2
should be(p: number) => number
.Actual behavior:
Type of function
comp
is(p: number) => number
which is correct.But type of function
comp2
is(p: never) => number
which is not correct.Also TypeScript shows the error:
Argument of type '<T>(x: T) => T' is not assignable to parameter of type '(p1: unknown) => number'
Playground Link: Link
The text was updated successfully, but these errors were encountered: