Closed
Description
Bug Report
🔎 Search Terms
intersection, contravariant, bivariant, Array, Set, union, covariant, assignability
🕗 Version & Regression Information
- This changed between versions 4.9.0-dev.20221013 and 4.9.0-dev.20221014
⏯ Playground Link
Playground link with relevant code
💻 Code
let x2: Set<(data: string) => void> & Set<(data: number) => void>
x2 = new Set<(data: string | number) => void>(); // okay 4.9.0-dev.20221013-, error 4.9.0-dev.20221014+
🙁 Actual behavior
The assignment fails in TS4.9+ with Type 'Set<(data: string | number) => void>' is not assignable to type 'Set<(data: string) => void> & Set<(data: number) => void>'.
and Types of property 'add' are incompatible.
🙂 Expected behavior
The assignment should presumably succeed as it did in TS 4.8-.
- Given that
Set<T>
is considered to be covariant inT
(despite being "morally" invariant due to methods likeadd()
), - and given that
(x: T) => void
is considered to be contravariant inT
, - then
Set<(x: T)=>void> & Set<(x: U)=>void>
should (probably?) be assignable toSet<(x: T | U) => void>
.
A Stack Overflow question has run into this.
It looks like #51140 might be responsible for the change.
This also occurs with Array<T>
or any type considered covariant but having bivariant methods; if you remove the bivariant methods from Set
then the assignment succeeds.