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 checker seems fine with this use of a generic type in a function parameter:
functioncallMe<T>(value: T,fn: (value: T)=>void) : void{fn(value);}// No error!callMe(17,(n: string)=>console.log(n));
If callMe is made non-generic and uses hardcoded types then an error is issued as expected:
functioncallMeConcrete(value: number,fn: (value: number)=>void) : void{fn(value);}// Error as expected!// TS2345: Argument of type '(n: string) => void' is not assignable to parameter of type '(value: number) => void'.callMeConcrete(17,(n: string)=>console.log(n));
The text was updated successfully, but these errors were encountered:
That is example considered by design due to function bivariance.
Basically, when we check assignability of a function, we check its parameter types in "both" directions. The type of [1, "foo"] is Array<number|string> so its element type is number|string, and the type number|string is assignable fromnumber, so the check succeeds.
We have an outstanding feature request #222 for turning off function argument type bivariance, which would make this an error.
The type checker seems fine with this use of a generic type in a function parameter:
If
callMe
is made non-generic and uses hardcoded types then an error is issued as expected:The text was updated successfully, but these errors were encountered: