Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Declared type as initial type in control flow analysis#8429

Merged
ahejlsberg merged 2 commits into
mastermicrosoft/TypeScript:masterfrom
declaredTypeAsInitialTypemicrosoft/TypeScript:declaredTypeAsInitialTypeCopy head branch name to clipboard
May 3, 2016
Merged

Declared type as initial type in control flow analysis#8429
ahejlsberg merged 2 commits into
mastermicrosoft/TypeScript:masterfrom
declaredTypeAsInitialTypemicrosoft/TypeScript:declaredTypeAsInitialTypeCopy head branch name to clipboard

Conversation

@ahejlsberg

Copy link
Copy Markdown
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

mhegazy commented May 3, 2016

Copy link
Copy Markdown
Contributor

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

@mhegazy

mhegazy commented May 3, 2016

Copy link
Copy Markdown
Contributor

👍

@ahejlsberg

Copy link
Copy Markdown
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

Morty Proxy This is a proxified and sanitized view of the page, visit original site.