10000 Assignability of covariant types with bivariant methods now fails in certain intersection types · Issue #51620 · microsoft/TypeScript · GitHub
[go: up one dir, main page]

Skip to content
Assignability of covariant types with bivariant methods now fails in certain intersection types #51620
Closed
@jcalz

Description

@jcalz

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 in T (despite being "morally" invariant due to methods like add()),
  • and given that (x: T) => void is considered to be contravariant in T,
  • then Set<(x: T)=>void> & Set<(x: U)=>void> should (probably?) be assignable to Set<(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 TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    0