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

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

Closed
realityfilter opened this issue Jul 8, 2016 · 2 comments
Closed
Labels
Duplicate An existing issue was already created

Comments

@realityfilter
Copy link

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.

@DanielRosenwasser DanielRosenwasser added Design Limitation Constraints of the existing architecture prevent this from being fixed Duplicate An existing issue was already created and removed Design Limitation Constraints of the existing architecture prevent this from being fixed labels Jul 8, 2016
@DanielRosenwasser
Copy link
Member

Marking as a duplicate of #7719.

The problem is that we can't guarantee that item continues being an ITextItem across certain function expression boundaries, because the invocation might be deferred, and something else might have changed the value in the mean time.

@glennsl
Copy link

glennsl commented Aug 15, 2016

It can be guaranteed if item is const though, and it does in fact compile if it is.

@mhegazy mhegazy closed this as completed Sep 20, 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

4 participants