-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Closed
Labels
BugA bug in TypeScriptA bug in TypeScriptFix AvailableA PR has been opened for this issueA PR has been opened for this issue
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) => voidis 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.
Metadata
Metadata
Assignees
Labels
BugA bug in TypeScriptA bug in TypeScriptFix AvailableA PR has been opened for this issueA PR has been opened for this issue