Skip to content
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

Types have no overlap ts(2367) error does not account for subtyping #48628

Closed
rpeszek opened this issue Apr 10, 2022 · 5 comments
Closed

Types have no overlap ts(2367) error does not account for subtyping #48628

rpeszek opened this issue Apr 10, 2022 · 5 comments
Labels
Duplicate An existing issue was already created

Comments

@rpeszek
Copy link

rpeszek commented Apr 10, 2022

Bug Report

🔎 Search Terms

is:issue overlap ts(2367)

(Could be relevant to #9998
However I have not seen equivalent example discussed there and seems to be a different topic.)

🕗 Version & Regression Information

Tested with 4.6.3 and 4.5.2.

⏯ Playground Link

Playground link with relevant code

💻 Code

/*
 * Compiler rejects `===` or `==` claiming that there is no type overlap where there is one.
 */
function testEqSemantics(a: {bye: string}, b: {hello: string}): boolean {
    //Compiliation error:
    //This condition will always return 'false' since the types '{ bye: string; }' and '{ hello: string; }' have no overlap. ts(2367)
    return a === b
 }

//valid overlap
const overlap = {hello: "world", bye: "Miss American Pie"}

//compiles
testEqSemantics(overlap, overlap)

🙁 Actual behavior

Compiler reject a valid overlap

🙂 Expected behavior

Needs discussion. At the minimum the error message needs to be changes as it is incorrect as is.

My preference would be to have ability to disable subtyping and keep that message when it is disabled.
This is actually doable today but is it cumbersome (https://rpeszek.github.io/posts/2022-01-09-ts-types-part4.html#safety-preventing-subtyping)

(As I stated before, this could be relevant to #9998
However I have not seen equivalent example discussed there and seems to be a different topic.)

@fatcerberus
Copy link

#9998 is unrelated, that's about control-flow analysis limitations across function boundaries which doesn't apply here. The blog post you linked talks about exact types, and there's already an issue for that - #12936.

In this case you essentially have a: Dog and b: Cat and TS is warning you about a direct comparison between the two (i.e. neither one is a subtype of the other, they are siblings within the type hierarchy). It's true that due to structural typing you might have a Dog & Cat on both sides but in most real-world cases an equality check between the two indicates a logic error, so the compiler flagging it is desirable.

For what it's worth, I am surprised that the error message claims that there's no overlap at all -- I had thought the wording was at one point "...neither type sufficiently overlaps with the other."

@RyanCavanaugh RyanCavanaugh added Working as Intended The behavior described is the intended behavior; this is not a bug Duplicate An existing issue was already created and removed Working as Intended The behavior described is the intended behavior; this is not a bug labels Apr 11, 2022
@RyanCavanaugh
Copy link
Member

See #27910, #31555, etc..

@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@rpeszek
Copy link
Author

rpeszek commented Apr 15, 2022

I am seeing linked tickets either marked as "Working as Intended" (#31555) or
Or as "Error Messages" (#27910).

The second one is over 3 years old. I am curious why this error message is not fixable.

@RyanCavanaugh
Copy link
Member

I am curious why this error message is not fixable.

It's entirely fixable. No one has sent a fix, and the team is busy doing things that only the team can do (versus rewording something, which pretty much anyone could do).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants