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

Type guards don't work with interface members #7593

Closed
notenoughneon opened this issue Mar 19, 2016 · 3 comments
Closed

Type guards don't work with interface members #7593

notenoughneon opened this issue Mar 19, 2016 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@notenoughneon
Copy link

TypeScript Version:

1.8.9

Code

interface I {
    m: string | {a: string};
}
var x: I;
var y: string;
if (typeof x.m === 'string') {
    y = x.m;
} else {
    y = x.m.a;
}

Expected behavior:
no compile errors

Actual behavior:
test.ts(7,5): error TS2322: Type 'string | { a: string; }' is not assignable to type 'string'.
Type '{ a: string; }' is not assignable to type 'string'.
test.ts(9,13): error TS2339: Property 'a' does not exist on type 'string | { a: string; }'.

Pulling x.m out into a separate variable causes the type guard to work as expected.

@yortus
Copy link
Contributor

yortus commented Mar 19, 2016

Duplicate of #3812?

@notenoughneon
Copy link
Author

Yes, I think it may be a duplicate. Sorry!

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Mar 21, 2016
@RyanCavanaugh
Copy link
Member

Yep, this should work once #3812 is fixed

@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