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

Misleading Error Message with == and valueOf #53757

Closed
SteveBeeblebrox opened this issue Apr 12, 2023 · 4 comments
Closed

Misleading Error Message with == and valueOf #53757

SteveBeeblebrox opened this issue Apr 12, 2023 · 4 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@SteveBeeblebrox
Copy link

Bug Report

πŸ”Ž Search Terms

always false, valueOf, equality

πŸ•— Version & Regression Information

  • This changed between versions 4.7.4 and 4.8.4 (and still happens on nightly)

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

console.clear()
console.log('foo' == {valueOf() {return 'foo'}}) // true

πŸ™ Actual behavior

The above code snippet produces an error on the == condition: This condition will always return 'false' since JavaScript compares objects by reference, not value.(2839) which would lead the user to believe that console.log(...) would always print false. In reality, the line prints true. The object on the right is coerced to a string using the valueOf() method so the comparison becomes 'foo' == 'foo' which is true, but TypeScript claims otherwise.

πŸ™‚ Expected behavior

Even if TypeScript cannot determine that this condition would be always true given the usage of valueOf, I would expect the error message to say something more like ...may always return false...
instead of the more certain ...will always return false... so that the user is not told that something will certainly happen when it may not.

@MartinJohns
Copy link
Contributor

I'd call this a duplicate of #2361. valueOf() is just not really supported by the type system.

@SteveBeeblebrox
Copy link
Author

I don't expect TypeScript to understand the valueOf(), but it would be nice if the error message wording didn't contradict runtime behavior.

@fatcerberus
Copy link

What do you propose? The error message is true if you name the method literally anything else, so you’re de facto asking typescript to understand the semantics of valueOf, which it currently doesn’t.

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Apr 12, 2023
@RyanCavanaugh
Copy link
Member

It's a waste of time and effort to make the message less clear for the sake of satisfying cases that can only be hit through specific construction.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

4 participants