Skip to content

CFA Assertions Not Active on Implicitly-Typed Function Calls #33580

@molisani

Description

@molisani

TypeScript Version: 3.7.0-insiders.20190923 (this tgz from this comment)

Search Terms: asserts cfa control flow analysis

Code

class Foo {
}

interface BarChild {
    parent: Bar;
}

class Bar {
    static staticAdd(f: Foo): asserts f is Foo & BarChild {}
    add(f: Foo): asserts f is Foo & BarChild {}
    innerTest() {
        const innerFoo = new Foo();
        this.add(innerFoo);
        innerFoo.parent;  // this compiles successfully
    }
}

function main() {
    const foo1 = new Foo();
    const bar1 = new Bar();
    bar1.add(foo1);
    foo1.parent;  // this is an ERROR - Property 'parent' does not exist on type 'Foo'. ts(2339)

    const foo2 = new Foo();
    Bar.staticAdd(foo2);
    foo2.parent;  // this compiles successfully
}

The Bar.add assertion works when called inside another method of Bar but fails when called externally from main. I added a static function assertion to show that assertion still works otherwise in main.

Expected behavior: In main after bar1.add(foo1), foo1 should be of type Foo & BarChild.

Actual behavior: In main after bar1.add(foo1), foo1 is still type Foo.

Playground Link: I've duplicated the sample code above in this playground link, but at the time of posting the nightly version does not have these changes.

Related Issues: #32695 This feature was merged to master yesterday, so instead of commenting on a closed PR I opened a new issue.

Metadata

Metadata

Assignees

Labels

Breaking ChangeWould introduce errors in existing codeBugA bug in TypeScript

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions