Skip to content

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

Closed
tinganho opened this issue Oct 4, 2017 · 6 comments
Closed

Expressions in typeof undefined checks should not error on undefined #18940

tinganho opened this issue Oct 4, 2017 · 6 comments
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@tinganho
Copy link
Contributor

tinganho commented Oct 4, 2017

I think typeof undefined checks should not error in the last accessor of the expression. Because I'm already checking for undefined:

So below code should be perfectly fine:

interface A {
    a: string;
}

interface B {
    b: string;
}

function isA(x: A | B): x is A {
    return typeof x.a !== 'undefined'; // Error in "a" does not exists
}

Right now, I have to type assert x to any.

@Jessidhia
Copy link

Jessidhia commented Oct 4, 2017

(You forgot to quote undefined, right now that check is always false)

This is a consequence of | types only allowing access to the subset of common known keys, because something that matches A is also allowed to coincidentally have a key that would have matched B, and vice-versa.

If you add b: never to A and a: never to B, this will allow you to test them for refinement even without needing an assertion function.

@tinganho
Copy link
Contributor Author

tinganho commented Oct 4, 2017

This is a suggestion to allow non-existing accessors to be allowed just in typeof undefined checks.

@ghost
Copy link

ghost commented Oct 4, 2017

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: "" }));

@tinganho
Copy link
Contributor Author

tinganho commented Oct 4, 2017

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 typeof undefined checks. Because you are checking undefined on runtime and the compiler shouldn't complain about a non-existent property.

@mhegazy
Copy link
Contributor

mhegazy commented Oct 4, 2017

related to #1260

@mhegazy mhegazy added the Design Limitation Constraints of the existing architecture prevent this from being fixed label Oct 4, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Oct 19, 2017

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@mhegazy mhegazy closed this as completed Oct 19, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed
Projects
None yet
Development

No branches or pull requests

3 participants