-
Notifications
You must be signed in to change notification settings - Fork 12.8k
TS enforces left-hand side of instanceof must be an object type, but it can be any type. #59492
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
Comments
I would hate to miss out on catching a certain class of errors just because of a technically-okay but extremely uncommon coding patterns. For example: function foo(value: string | number) {
if (value instanceof Number) {
// oops - never runs!
}
} Can you provide more details on why you want this behavior? |
It's true that it's not a common pattern, and probably anyone doing it is abusing the language, but it's part of the spec and all runtimes support it. I'm really not eager to have this fixed, and I perfectly understand if it's not prioritised or even if it gets closed. I just identified that this is actually valid JS, yet TS will not allow it, giving back an error that's actually incorrect. The left-hand side can be any value.
Fixing this doesn't mean removing the error altogether, which I agree it's very useful. A possible fix would be to allow for any type of value as long as the right-hand side is an object with |
This isn't really the bar for what TS has to do.
But If this issue is just a "looking for problems and successfully finding one" thing then I think we should just close rather than spend time investigating possible options. |
I mean, the very point of TypeScript is to prevent "valid" JavaScript. |
There's a big difference though, which is that Math.max in this case will always return NaN. It was not meant to work with strings. Now, if the spec for Math.max also included strings, or allowed some kind of configuration that would allow it, so that it returns the string furthest down the alphabet ("world") in this case, should TS forbid using strings because it says This is my point. The original example with even numbers is a valid case for Maybe another point of TS doing a good job that's actually a better one is in the following: function foo() {
return 3
}
if (foo) {
// ....
} Of course it's valid JS, yet TS will show an error. But this is because TS knows that the But again, the big difference here is that I feel like I'm playing devil's advocate here. This is never a pattern I will find acceptable to use other than in playgrounds / experimental esoteric stuff. Feel free to reopen it if you actually see value in it, or someone comes with a non-contrived use case. |
π Search Terms
"instanceof", "Symbol.hasInstance"
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/play/?ts=5.5.4#code/FAYw9gdgzgLgBAUQG4FMIDkCuBbARigJyjgF44BvYOOAbQGUBPPMAGwDoALAQygEloYXCCBQBdAFxwAFEklCGASlIA+ODAYAHFGABmcJKRJkA5BBz4CxuADJr+uAFI4AJkNwADMAC+AbmDAASz0pAGY4AIEhEV1EVAxzQiglSmpwaFYUNhYwAHMpY2wODjgAd24YAH5jBW9-IOkARndwyOFtPWQ0LDxE5Ko4NKgMrNypACIm8OIUOLGar2AgA
π» Code
π Actual behavior
Typescript shows an error on the
3
and the10
:π Expected behavior
It should not show any error, because the code is valid JS
Additional information about the issue
After having support for
hasInstance
#55052, this actually means that the left-hand side operand doesn't have to be an object anymore. It can perfectly be primitives, as shown by this example (albeit contrieved...)The spec for relational expressions also allows any kind of relational expressions (which also includes anything that resolves in a primitive) as well https://tc39.es/ecma262/multipage/ecmascript-language-expressions.html#sec-relational-operators
The text was updated successfully, but these errors were encountered: