-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Expressions in typeof undefined checks should not error on undefined #18940
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
(You forgot to quote This is a consequence of If you add |
This is a suggestion to allow non-existing accessors to be allowed just in typeof undefined checks. |
Without exact types (#12936) this is unsound. interface A {
a: string;
}
interface B {
b: string;
}
function isA(x: A | B): x is A {
return typeof (x as any).a !== 'undefined';
}
function f(value: A | B): string {
return isA(value) ? value.a.toUpperCase() : value.b.toLowerCase();
}
// Crashes on `1.toUpperCase()`
console.log(f({ a: 1, b: "" })); |
Even though it is unsound, my suggestion is still to allow one to write: typeof x.a !== 'undefined'; Instead of having to: typeof (x as any).a !== 'undefined'; And this should only work on |
related to #1260 |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
I think
typeof undefined
checks should not error in the last accessor of the expression. Because I'm already checking forundefined
:So below code should be perfectly fine:
Right now, I have to type assert
x
to any.The text was updated successfully, but these errors were encountered: