Skip to content

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

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.

Activity

  1. added
    Design LimitationConstraints of the existing architecture prevent this from being fixed
    DuplicateAn existing issue was already created
    and removed
    Design LimitationConstraints of the existing architecture prevent this from being fixed
    on Jul 8, 2016
  2. DanielRosenwasser commented on Jul 8, 2016

    @DanielRosenwasser
    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.

  3. glennsl commented on Aug 15, 2016

    @glennsl

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

  4. locked and limited conversation to collaborators on Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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