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
Disallow intersection of exact objects #8158
Comments
There are corner cases where the intersection type is not empty:
or
( But I agree that warning against empty intersections of exact types would be useful in most cases and to a lot of people. |
@SillyFreak Interesting, hadn't thought about those cases! Anyhow, do you think there would be any real world use cases with them that couldn't be achieved easily in other ways? |
real world use cases I'm not sure, but disallowing intersection types in this specific circumstance, because there are workarounds for when this is actually the desired behavior seems like the wrong approach. Maybe a lint would make sense, but disallowing this seems too harsh. In principle it should be possible to determine when the resulting type is empty, and disallowing such types (including
|
Proposal
Intersections of exact objects does not make sense since they produce impossible type that can't be used.
{| a: number|} & {| b: string |}
produces an object type that only has propertya
and nothing else, but also has propertyb
and nothing else. This indicates that intersections can only be used for inexact objects.In optimal case the error message would be describing and maybe communicate why intersection of exact objects can't be used and suggest to use spreading instead.
This is important especially for newcomers and people who are used to write TS. I find my self very often explaining how and why intersection of exact objects can't be used.
I understand that this might not be big deal for FB, but for community it's important to make experience good for newcomers.
The text was updated successfully, but these errors were encountered: