Skip to content

[5.0] Intersected array of intersection types with union field not compatible #53412

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
intgr opened this issue Mar 21, 2023 · 3 comments · Fixed by #56207
Closed

[5.0] Intersected array of intersection types with union field not compatible #53412

intgr opened this issue Mar 21, 2023 · 3 comments · Fixed by #56207
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue

Comments

@intgr
Copy link

intgr commented Mar 21, 2023

Bug Report

Basically I'm using & types as if subclassing. BaseValue has items array with fewer properties, ExtendedValue extends that with items with more properties.

🔎 Search Terms

intersection, ampersand, override, inheritance. Also looked at the titles of first 2 pages of open issues.

🕗 Version & Regression Information

  • This changed between versions 4.9.5 and 5.0.2
  • There is an easy work-around, but I'm not sure if it should be necessary.

⏯ Playground Link

TypeScript 5.0.2 (error) vs 4.9.5 (OK)

💻 Code

type BaseItem = {
  id: number;
}
type ExtendedItem = BaseItem & {
  description: string | null
};


type BaseValue = {
  // there are other fields
  items: BaseItem[];
}
type ExtendedValue = BaseValue & {
  // there are other fields
  items: ExtendedItem[];
}


const TEST_VALUE: ExtendedValue = {
  items: [  // ❌ ERROR: This is NOT allowed *iff* mixed types are used in `description`
    {id: 1, description: null},
    {id: 2, description: 'wigglytubble'},
  ]
};

🙁 Actual behavior

Seems to me like they should be compatible. Also these examples are ✅ allowed in TS 5.0.2 if description only has uniform types, e.g.:

// ✅ Allowed: description has only strings
const ONLY_STRINGS: ExtendedValue = {
  items: [
    {id: 1, description: 'bollywoggle'},
    {id: 2, description: 'wigglytubble'},
  ]
};
// ✅ Allowed: description has only nulls
const ONLY_NULLS: ExtendedValue = {
  items: [
    {id: 1, description: null},
    {id: 2, description: null},
  ]
};

🙂 Expected behavior

Seems like it should be allowed.

🙄 Work-around

Typing ExtendedValue with Omit<> seems to fix the issue with no obvious downsides.

type ExtendedValue = Omit<BaseValue, 'items'> & {
  // there are other fields
  items: ExtendedItem[];
}
@typescript-bot
Copy link
Collaborator

The change between origin/release-4.9 and origin/release-5.0 occurred at 3099385.

@RyanCavanaugh
Copy link
Member

#52392

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label Mar 21, 2023
@ahejlsberg ahejlsberg added Bug A bug in TypeScript and removed Needs Investigation This issue needs a team member to investigate its status. labels Oct 24, 2023
@ahejlsberg
Copy link
Member

This issue looks to be caused by incomplete propagation of the intersectionState parameter in our type relation logic. I have a fix that I'll put up shortly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants