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

User-defined type guards seemingly unused when given an intersection type #10144

Closed
TyOverby opened this issue Aug 4, 2016 · 2 comments
Closed
Labels
Fixed A PR has been merged for this issue

Comments

@TyOverby
Copy link

TyOverby commented Aug 4, 2016

TypeScript Version: 1.8.10

Code

interface Foo {
    x: number;
    s: string;
}

interface Bar {
    y: number;
    z: number;
}

interface HasNum {
    a: number;
}

type FooOrBar = Foo | Bar;

type FooOrBarWithNum = FooOrBar & HasNum;

function is_foo(f: FooOrBar): f is Foo {
    return (f as Foo).x !== undefined;
}

let obj: FooOrBarWithNum = {
    a: 5,
    x: 5,
    s: "hello"
};

if (is_foo(obj)) {
    obj.x; // ERROR: Property 'x' does not exist on type (Foo | Bar) & HasNum
}

let obj_2: FooOrBar = obj; // Cast it back to just a FooOrBar.

if (is_foo(obj_2)) {
    obj_2.x; // This compiles just fine.
}

Expected behavior:
I would expect obj to have type Foo inside of the if-block.

Actual behavior:
obj is not refined, but obj_2 is.

@TyOverby TyOverby changed the title User-defined type guards broken when used with Intersection Types User-defined type guards seemingly unused when used with Intersection Types Aug 4, 2016
@TyOverby TyOverby changed the title User-defined type guards seemingly unused when used with Intersection Types User-defined type guards seemingly unused when given an intersection type Aug 4, 2016
@yortus
Copy link
Contributor

yortus commented Aug 4, 2016

This looks related to or a duplicate of #9919

@yortus
Copy link
Contributor

yortus commented Oct 21, 2016

This works now. Fixed by #11717.

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label May 24, 2017
@weswigham weswigham added Fixed A PR has been merged for this issue and removed Needs Investigation This issue needs a team member to investigate its status. labels Nov 29, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

4 participants