Skip to content

Declared type as initial type in control flow analysis #8429

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

Merged
merged 2 commits into from
May 3, 2016

Conversation

ahejlsberg
Copy link
Member

With this PR we consider the initial type of a variable to be a union of its declared type and undefined. This makes control flow based type analysis less aggressive in cases where the checker is unable to see assignments to a variable because they exclusively occur in nested functions (which aren't included in the analysis). For example:

let result: Foo | undefined;
scanForResult();
if (result) {
    // Do something with result
}

function scanForResult() {
    // Logic that possibly assigns to result
}

With the PR we consider the initial type of result to be Foo | undefined, which narrows to Foo in the type guard. Previously we considered the initial type to be undefined which would narrow to nothing and cause errors.

@mhegazy
Copy link
Contributor

mhegazy commented May 3, 2016

hmm.. non of the baselines changed.. did not expect that.

@mhegazy
Copy link
Contributor

mhegazy commented May 3, 2016

👍

@ahejlsberg
Copy link
Member Author

@mhegazy Yeah, but that's partially because we don't have a lot of tests with --strictNullChecks. Still, it seems that it is rare to rely on the initial value in an uninitialized variable. Which, now that I write it, doesn't actually seem to surprising. 😄

@ahejlsberg ahejlsberg merged commit 2ff9c91 into master May 3, 2016
@mhegazy mhegazy deleted the declaredTypeAsInitialType branch May 10, 2016 21:24
@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
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants