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

IIFEs incorrectly refine Maybe-values to Nothing #8381

Closed
RReverser opened this issue Apr 29, 2016 · 2 comments
Closed

IIFEs incorrectly refine Maybe-values to Nothing #8381

RReverser opened this issue Apr 29, 2016 · 2 comments
Assignees
Labels
Bug A bug in TypeScript

Comments

@RReverser
Copy link
Contributor

RReverser commented Apr 29, 2016

TypeScript Version:

nightly (1.9.0-dev.20160429)

Code

let maybeNumber: number | undefined;
(function () {
    maybeNumber = 1;
})();
if (maybeNumber !== undefined) {
    maybeNumber++;
}

Expected behavior:
Everything works without errors, maybeNumber preserves type of number | undefined and refines to number in the if block.

let maybeNumber: number | undefined;
// Control flow figured that: maybeNumber === undefined
(function () {
    maybeNumber = 1;
})(); // --> oops, unknown side effects
// Revert maybeNumber to initial `number | undefined` to cover all possibilities
if (maybeNumber) {
    // Control flow: can refine to `number` again here
    maybeNumber++;
}

Actual behavior:
Language service reports type nothing on maybeNumber after the call (even though it's number | undefined before the call), so statement inside of the if condition breaks.

@tinganho
Copy link
Contributor

I just encountered something similar.

let test: string | undefined;
if (!test) {
    throw new Error('Test is not defined');
}
(() => {
    test.slice(1); // error possibly undefined...
})();

@zpdDG4gta8XKpMCd
Copy link

@ahejlsberg would you consider additional knowledge of functions being pure to confidently do flow analysis for certain cases? #7770

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript
Projects
None yet
Development

No branches or pull requests

6 participants