Skip to content

Array of intersection is not intersection of arraysΒ #62505

@jsalvata

Description

@jsalvata

πŸ”Ž Search Terms

array intersection [] & distributive law

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about Type System Behaviour.

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.9.2#code/C4TwDgpgBAggTnAhiA8gMwJIDtgTgZwgGNgBLAeyygF5YFkAeAbygA8AuKLAVwFsAjPFAC+UAGRQWITvmBxSWAOYiAfAG4AsAChQkKNlwFiZSunhIQ+GnQvM2nHgKHCV4m4yky5C5S81btABsIYChEclJOc2R0AzxCEgosf2DQikROOKNE0zRoy39tNG4sHKpcWQAKAEpJbSgodOtw0n8Glut0-2FtIA

πŸ’» Code

type ArrayOfIntersection = Array<{ x: number } & { y: string }>;
type IntersectionOfArrays = Array<{ x: number }> & Array<{ y: string }>;

let aoi: ArrayOfIntersection;
let ioa: IntersectionOfArrays;

function test() {
  ioa = aoi;
  aoi = ioa; // Type error
}

πŸ™ Actual behavior

The aoi = ioa assignement has a type error, while Array<A & B> should be exactly equal to Array<A> & Array<B>:

  • Values in an Array<A & B> have type A & B.
  • Values in an Array<A> & Array<B> have type A and also have type B -- that is: they have type A & B.
    So these two types are equivalent, yet Typescript accepts assignment in one direction but not the other.

πŸ™‚ Expected behavior

The above code should compile without error.

Additional information about the issue

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions