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 analysis should always be performed on bare this keywords #21248

Closed
weswigham opened this issue Jan 18, 2018 · 0 comments
Closed
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@weswigham
Copy link
Member

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.

@DanielRosenwasser DanielRosenwasser added the Bug A bug in TypeScript label Jan 18, 2018
@DanielRosenwasser DanielRosenwasser added this to the TypeScript 2.8 milestone Jan 18, 2018
@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Feb 8, 2018
@microsoft microsoft locked and limited conversation to collaborators Jul 3, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

3 participants