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

Union Type Guard Breaks If Expression Has Property #7288

Closed
Joncom opened this issue Feb 29, 2016 · 2 comments
Closed

Union Type Guard Breaks If Expression Has Property #7288

Joncom opened this issue Feb 29, 2016 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@Joncom
Copy link

Joncom commented Feb 29, 2016

TypeScript Version:

1.8.2

Code

interface Foo {
    property: (boolean | { bar: number })
}

var foo: Foo = {
    property: { bar: 123 }
};

var p = foo.property;
if (typeof p === 'object') {
    // This works fine!
    console.log(p.bar); // Prints "123"
}

if (typeof foo.property === 'object') {
    // This line produces the following compilation error:
    // "Property 'bar' does not exist on type 'boolean | { bar: number; }'"
    console.log(foo.property.bar);
}

Expected behavior:

I expected if (typeof foo.property === 'object') { to be a valid type guard.

Actual behavior:

It's not.

@yortus
Copy link
Contributor

yortus commented Feb 29, 2016

Duplicate of #3812?

@DanielRosenwasser
Copy link
Member

Thanks for digging that up @yortus!

@DanielRosenwasser DanielRosenwasser added the Duplicate An existing issue was already created label Feb 29, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants