🔎 Search Terms
unsound generic function assignability type parameter variance generic call signature bivariant
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about generics and variance.
⏯ Playground Link
https://www.typescriptlang.org/play/?ts=6.0.3#code/C4TwDgpgBAygFgewK4BsAmAhCBJAdgNwEMAnAS0N2AB4ARAPigF4oqAVOgCgA8AuKVgGQ0ANFBB9WASkZ1BNAFDyAxglwBnYFDUQVuNH3jJ0WPETIVqSXAGtcCAO64GzKN1EhJTBiGWqNUZGAAOSQAWwAjCGIDRFRMHAISckoqXDDI4mctHVU0eUCQiKiqK1sHJw4AFlEAcg1iGskAOmAEAFEuMFUISnIUDklFXX9SXEKMmKN40ySLVPSorO5PGVcuFvbO7t7CfslRLkHhzVGAVRs7R0m4k0TzFNLLpyYoUfGo+TOL8o464AbavVGoogA
💻 Code
type ShouldBeInvariant<D> = <T>(x: T&D, y: T)=>T&D
const second: ShouldBeInvariant<unknown> = (x, y) => y
const outNumber: ShouldBeInvariant<number> = second
outNumber<unknown>(4, 'str').toExponential()
const inNumber: ShouldBeInvariant<number> = (x) => (x.toExponential(), x)
const inUnknown: ShouldBeInvariant<unknown> = inNumber
inUnknown('str', 'str')
🙁 Actual behavior
The code type checks, while it shouldn't because that is unsound, using ShouldBeInvariant covariantly or contravariantly both causes runtime errors.
🙂 Expected behavior
ShouldBeInvariant should be invariant against its parameter. Both assigns:
const outNumber: ShouldBeInvariant<number> = second
and
const inUnknown: ShouldBeInvariant<unknown> = inNumber
should report errors
Additional information about the issue
No response
🔎 Search Terms
unsound generic function assignability type parameter variance generic call signature bivariant
🕗 Version & Regression Information
⏯ Playground Link
https://www.typescriptlang.org/play/?ts=6.0.3#code/C4TwDgpgBAygFgewK4BsAmAhCBJAdgNwEMAnAS0N2AB4ARAPigF4oqAVOgCgA8AuKVgGQ0ANFBB9WASkZ1BNAFDyAxglwBnYFDUQVuNH3jJ0WPETIVqSXAGtcCAO64GzKN1EhJTBiGWqNUZGAAOSQAWwAjCGIDRFRMHAISckoqXDDI4mctHVU0eUCQiKiqK1sHJw4AFlEAcg1iGskAOmAEAFEuMFUISnIUDklFXX9SXEKMmKN40ySLVPSorO5PGVcuFvbO7t7CfslRLkHhzVGAVRs7R0m4k0TzFNLLpyYoUfGo+TOL8o464AbavVGoogA
💻 Code
🙁 Actual behavior
The code type checks, while it shouldn't because that is unsound, using
ShouldBeInvariantcovariantly or contravariantly both causes runtime errors.🙂 Expected behavior
ShouldBeInvariantshould be invariant against its parameter. Both assigns:and
should report errors
Additional information about the issue
No response