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

Feature Request: Infer types when using variable assignment with if-statements. #19421

Closed
ccorcos opened this issue Oct 23, 2017 · 1 comment · Fixed by #44730
Closed

Feature Request: Infer types when using variable assignment with if-statements. #19421

ccorcos opened this issue Oct 23, 2017 · 1 comment · Fixed by #44730
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed Duplicate An existing issue was already created

Comments

@ccorcos
Copy link

ccorcos commented Oct 23, 2017

TypeScript Version: 2.6.0-dev.201xxxxx

Code

function f(x: string | undefined) {
	if (x) {
		const l = x.length
	}
	const y = !!x
	if (y) {
                // type error with strictNullChecks on.
		const l = x.length
	}
	if (!!x) {
		const l = x.length
	}
}

Expected behavior:

I would expect this code to compile without errors by simply refactoring some conditions into variables.

Actual behavior:

Unless the entire condition lives inside the if statement, there will be a type error.

@ahejlsberg
Copy link
Member

This is a design limitation. The control flow analyzer doesn't track what effects a particular value for one variable implies for other variables. See #12184 for a similar example.

@ahejlsberg ahejlsberg added Duplicate An existing issue was already created Design Limitation Constraints of the existing architecture prevent this from being fixed labels Oct 23, 2017
@mhegazy mhegazy closed this as completed Oct 23, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed Duplicate An existing issue was already created
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants