Skip to content

function discards outer type-check on union type #13560

Closed
@MrAndersen1

Description

@MrAndersen1

TypeScript Version: 2.2.0-dev.20170113

Code

class test1 {
	someProp;
}
class test2 {
	anotherProp;
}

let myTest: test1 | test2;
if (myTest instanceof test1) {
	// valid
	myTest.someProp;

	// invalid, someProp does not exist on type 'test2'
	() => myTest.someProp;

	// meaning if you have a eg. forEach
	[].forEach(() => {
		// you will need an instance check here too
		// invalid
		myTest.someProp;

		// valid
		if (myTest instanceof test1)
			myTest.someProp;
	})
}

Expected behavior:
I expected typescript to know myTest was instanceof test1 inside the function. It did not give these errors in typescript 1.8

Actual behavior:
It lost this knowledge inside the function.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn 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