Skip to content

if statement in loop changes type of variable outside the if #19955

Closed
@ghost

Description

TypeScript Version: 2.7.0-dev.20171112

Code

type U = { kind: "A", xs?: number[] } | { kind: "B", xs?: number[] };
function f(u: U) {
	if (u.xs) {
		for (const b of u.xs) {
			if (u.kind === "A") {}
		}
	}
}

Expected behavior:

No error.

Actual behavior:

src/a.ts(4,19): error TS2532: Object is possibly 'undefined'.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions