Skip to content

Control Flow Analysis + Getter Properties #12070

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

Closed
mariusschulz opened this issue Nov 6, 2016 · 2 comments
Closed

Control Flow Analysis + Getter Properties #12070

mariusschulz opened this issue Nov 6, 2016 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@mariusschulz
Copy link
Contributor

TypeScript Version: 2.0.3 / nightly (2.1.0-dev.20161106)

The following example doesn't compile without errors in the most recent TypeScript nightly build. Control flow analysis determines that scanner.current has the type "/", and thus TypeScript flags the string comparison with "*". However, it doesn't take into account that current is a getter property, which returns a different value after advance() has been called.

Wouldn't it make sense to treat getter properties like methods in this case and not narrow their type to a string literal type?

Code

class Scanner {
    constructor(private input: string, private pos = 0) {}

    advance() {
        this.pos++;
    }

    get current() {
        return this.input[this.pos];
    }
}

const scanner = new Scanner("/* Comment */");

if (scanner.current === "/") {
    scanner.advance();

    if (scanner.current === "*") {
        // Error
        // Operator '===' cannot be applied to types '"/"' and '"*"'
    }
}

Expected behavior:
No errors.

Actual behavior:
Error Operator '===' cannot be applied to types '"/"' and '"*"'

@aluanhaddad
Copy link
Contributor

aluanhaddad commented Nov 6, 2016

This is not really related to the property being computed or even to its being a property at all. Calling any function could have arbitrary side effects that cannot plausibly be tracked. Here is a minimal repro

let current = "/* comment */";

const advance = () => {
    current = current.slice(1);
};

if (current === "/") {

    advance();

    if (current === "*") { // [ts] Operator '===' cannot be applied to types '"/"' and '"*"'
    }
}

Edit: see #9998

@mariusschulz
Copy link
Contributor Author

mariusschulz commented Nov 6, 2016

Thank your for linking to #9998. For interested readers, Anders precisely answers my questions here. Interestingly, the same issue came up in the TypeScript compiler (advancing to the next token).

tl;dr: It's a calculated tradeoff of control flow analysis.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Nov 10, 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

3 participants