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

function discards outer type-check on union type #13560

Closed
MrAndersen1 opened this issue Jan 18, 2017 · 3 comments · Fixed by #56908
Closed

function discards outer type-check on union type #13560

MrAndersen1 opened this issue Jan 18, 2017 · 3 comments · Fixed by #56908
Labels
Duplicate An existing issue was already created

Comments

@MrAndersen1
Copy link

MrAndersen1 commented Jan 18, 2017

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.

@HerringtonDarkholme
Copy link
Contributor

Possible duplicate of #9998

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jan 18, 2017
@RyanCavanaugh
Copy link
Member

#9998 / #11498

@MrAndersen1
Copy link
Author

Ah, yes, probably. My search for similar issues was not good enough it seems. Thanks!
Interesting read.

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants