Skip to content

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

Closed
@ccorcos

Description

@ccorcos

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixedDuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions