Skip to content

control flow based type analysis for type guards broken in 2.0 dev build #9569

Closed
@realityfilter

Description

@realityfilter

TypeScript Version: nightly (Version 2.0.0-dev.20160707)

Code

interface IItem {
    id: string;
}

interface ITextItem extends IItem {
    text: string;
}

function isText(item: IItem): item is ITextItem {
    return item['text'] != null;
}

interface IContent {
    items: IItem[];
}

let contents = [] as IContent[];
let item = { id: 'id', text: '\n' } as IItem;

if (isText(item)) {
    console.log(item.text);
    contents.forEach(content => {
        content.items.forEach(i => {
            if (isText(i) && i.text === item.text) {
                // do soemthing
            }
        });
    });
}

Expected behavior:

Compilation without errors like with version 1.8.10

Actual behavior:

Error during compilation:
test.ts(24,49): error TS2339: Property 'text' does not exist on type 'Item'.

The first reference of item.text is working correctly. The second in the lambda function in the forEach not.

This might by related to #8010.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions