Skip to content

The behavior of the intersection type of array changed in 2.6.1 #20001

Closed
@icepeng

Description

@icepeng

TypeScript Version: 2.6.1

Code

interface A {
  first: string;
  bList?: B[];
}

interface B {
  second: string;
  c?: C;
}

interface C {
  third: string;
}

const Q: A & { bList: (B & { c: C })[] } = {
  first: 'first',
  bList: [
    {
      second: 'second',
      c: {
        third: 'third',
      },
    },
  ],
};

Q.bList.map(item => {
  P(item);
});

function P(value: B & { c: C }) {
  console.log(value);
}

Expected behavior:
Until 2.5.1 this code did not generate a compilation error.
The type of bList is calculated as follows:

(undefined & (B & {
  c: C;
})[]) | (B[] & (B & {
  c: C;
})[])

And type of item

B & {
  c: C;
}

Actual behavior:
With 2.6.x, this code causes an error.
The type of bList is calculated as follows:

B[] & (B & {
  c: C[];
})[]

And type of item

B

The type of item is treated as B and an error occurs. Is this change specified in the changelog?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions