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

Number type value arguments falsly claim to not over overlap #49247

Closed
Caleb-T-Owens opened this issue May 25, 2022 · 4 comments
Closed

Number type value arguments falsly claim to not over overlap #49247

Caleb-T-Owens opened this issue May 25, 2022 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@Caleb-T-Owens
Copy link

Bug Report

πŸ”Ž Search Terms

Type Value Comparison Type Domain Overlap TS2367

πŸ•— Version & Regression Information

  • This is an incorrect compiler error
  • This behaviour was observed in every version I tried

⏯ Playground Link

Link to the example code

πŸ’» Code

type Tuple<T, Length extends number> = [T, ...T[]] & { length: Length }
type Table<T, Width extends number, Height extends number> = Tuple<Tuple<T, Width>, Height>

class Matrix<Width extends number, Height extends number> {
    constructor(public data: Table<number, Width, Height>) {}

    get isSquare() {
        return this.data.length === this.data[0].length
    }
}

πŸ™ Actual behaviour

Causes error TS2367: This condition will always return 'false' since the types 'number & Height' and 'number & Width' have no overlap.
There seems to be several issues here:

  • Why is number & Height not absorbed into just Height? - Might not be a bug
  • Why is it number & Height in the first place? - Might not be a bug
  • Why is there an error occurring here? Both Width and Height extend number and thus there should be ample overlap between the two types. - Defiantly an issue.

My best guess at what is occurring is the type checker doesn't have the capability to look at the "parent" of Width and Height and notice that they both have the same "parent"

Casting the types to Width and Height and doing the comparison still has the domain error.

πŸ™‚ Expected behaviour

No errors occur

@jcalz
Copy link
Contributor

jcalz commented May 25, 2022

Related to #47026; comparisons between two generic types are not usually allowed.

The wording of the error message "This condition will always return false because the types Foo and Bar don't overlap" isn't great, in my opinion. I'd prefer something like "Types Foo and Bar are not directly related to each other and comparing them is probably a mistake; if this is intentional, please assert one or both of the expressions to a common type like Foo | Bar." I guess that message is probably going to stay, though... #26592 is open, but #36534 is closed as "won't fix"

@Caleb-T-Owens
Copy link
Author

Its certainly disappointing that any checking doesn't occur, but I guess the complexity of the type checker needs to be considered, but it certainly should have a better compiler message needs to be used because it is a valid statement.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label May 25, 2022
@RyanCavanaugh
Copy link
Member

I think as part of #26592 we need to remove the "will always return false" verbiage

@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.

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