Skip to content

Reduce empty intersections to never #18210

Closed
@leoasis

Description

@leoasis

TypeScript Version: 2.4.0

Code

type A = {
    foo?: {
        bar?: string;
    }
}

type B = {
    foo?: {
        baz?: number
    };
}

type C = A & B;

let c: C = { foo: { bar: '123', baz: 123 } };

if (c.foo) {
    console.log(c.foo.bar); // throws error
}

Expected behavior:

This should not raise an error, as I've already checked for existance of foo in c, so the object in foo should have both bar and baz as optional with their respective types. The type should be refined because I've already checked for the existance of foo.

Actual behavior:

Typescript shows an error when trying to access either bar or baz, as each property doesn't exist in one of the combinations of the intersection type, but the combination includes foo being undefined, which I checked above.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions