-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Add 'no instanceof' config switch #16693
Comments
Since this is valid JavaScript and would be a coding convention, like using |
Agreed with @kitsonk - disallowing these sorts of constructs is not something we generally do. |
I assume a1.a = 'hello';
function safe(x: any) {
if (x instanceof A) {
return x.a; // safe
}
return 'who knows';
}
safe(a1); // 'hello'
safe(a2); // 'who knows' but not safe in this direction: function unsafe(x: A | string) {
if (x instanceof A) {
return x.a.concat('!');
}
return x.concat('!'); // not necessarily
}
unsafe(a1); // 'hello!'
unsafe(a2); // TypeError: x.concat is not a function It would be nice if any linter warning didn't flag the first usage. |
That would be up to the linter. |
@jcalz In your second example the difference between the way ts perceives |
The way
instanceof
works presents a potential pitfall for ts developers. An instance passing ts type check may (or may not) failinstanceof
check depending on the way it was constructed:While this may not be avoidable given the relationship between js and ts, it might be useful to have the compiler enforce a policy where using
instanceof
is simply disallowed.The text was updated successfully, but these errors were encountered: