Skip to content

Control flow analysis should always be performed on bare this keywords #21248

Closed
@weswigham

Description

@weswigham

TypeScript Version: 2.7.0-dev.201xxxxx

Code

function bar(this: string | number) {
    if (typeof this === "string") {
        const x: number = this;
    }
}

Expected behavior:
Assignment succeeds.

Actual behavior:
Error, string | number is not assignable to number.

Also:

declare function isBig(x: any): x is typeof x & { big: true };
function bigger(this: object) {
    if (isBig(this)) {
        this.big; // Expect property to exist
    }
}

The type guard does nothing, but it should narrow and make the big member available within the true branch of the if statement.

We do some CFA on this when it is a this type from a class declaration and we want to check for definite assignment, but we're conspicuously lacking any calls to getFlowTypeOfReference in any other code paths in checkThisExpression. I'm pretty sure isMatchingReference already should handle this, too, since it needed to in order to handle narrowing this.foo correctly.

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