Skip to content

Strange narrowing behavior for types assigned to in while loop #29513

@midiusRed

Description

@midiusRed

TypeScript Version: 3.2.0+

Code

class AInfo {
	a_count:number = 1;
}
class BInfo {
	b_count:number = 1;
}

class Base {
	next:Base;
	id:number = 0;
}
class A extends Base {
	constructor(readonly info:AInfo) {
		super();
	}
}
class B extends Base {
	constructor(readonly info:BInfo) {
		super();
	}
}

let target:Base = ...;
while (target) {
	if (target instanceof A) {
		target.info.a_count = 3;
	} else if (target instanceof B) {
		target.info.b_count = 3;
	}
	target = target.next;
}

Expected behavior:
TS2551: Property 'b_count' does not exist on type 'AInfo'. Did you mean 'a_count'?
When code place in WHILE

error

Actual behavior:
no error

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