Skip to content

Intersection Type not merging objects in array properly #52073

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
ErlendS opened this issue Jan 2, 2023 · 5 comments
Closed

Intersection Type not merging objects in array properly #52073

ErlendS opened this issue Jan 2, 2023 · 5 comments

Comments

@ErlendS
Copy link

ErlendS commented Jan 2, 2023

Bug Report

πŸ”Ž Search Terms

intersection type, intersection type array

πŸ•— Version & Regression Information

  • This changed between versions 4.8.4 and 4.9.4
  • This bug also appears in version 3.5.1, 3.3.3

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

interface Colorful {
  color: string;
}
interface Circle {
  radius: number;
}

type ColorfulCircle = Colorful[] & Circle[];

const test: ColorfulCircle = [{
  color: 'red',
  radius: 2
}]

πŸ™ Actual behavior

radius field gets type error: Object literal may only specify known properties, and 'radius' does not exist in type 'Colorful'

It looks like it is treating it as a union type where only one of the fields can exist.

πŸ™‚ Expected behavior

I expected the merging of the types to make color and radius accessible (and required) in the ColorfulCircle type.

@fatcerberus
Copy link

ColorfulCircle should be defined as (Colorful & Circle)[].

@ErlendS
Copy link
Author

ErlendS commented Jan 2, 2023

ColorfulCircle should be defined as (Colorful & Circle)[].

Thanks for replying. That would fix this, but it is a contrived example to show the issue. I can't use that fix in the real issue I'm having.

What I am wondering is if the error I am getting now is a bug introduced in TS 4.9 since that used to work. (and the issue also shows up in two other versions, and then seems to be fixed again)

Looking at specs (though depricated) it looks like the intended behaviour of the intersection type would be to merge the two types to something like: Array<{color: string, radius: number}>

@jcalz
Copy link
Contributor

jcalz commented Jan 3, 2023

Duplicate of #41874

@ErlendS
Copy link
Author

ErlendS commented Jan 3, 2023

Yes, sorry I didn't find that when searching. Will close this one then.

@ErlendS ErlendS closed this as completed Jan 3, 2023
@ErlendS
Copy link
Author

ErlendS commented Jan 3, 2023

by the way @jcalz, any idea why my code example seems to work on version 4.8, but not 4.9?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants