Skip to content

Strange type narrowing with Discriminated union types #9254

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

Closed
Lenne231 opened this issue Jun 19, 2016 · 3 comments
Closed

Strange type narrowing with Discriminated union types #9254

Lenne231 opened this issue Jun 19, 2016 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@Lenne231
Copy link

TypeScript Version:

nightly (1.9.0-dev.20160619-1.0)

image

Expected behavior:

Type of shape should be never or Shape so that we can still access type to throw an Error.

  throw new Error('Unknown type. Type: ' + shape.type);

Actual behavior:

Type of shape is { type: 'rectangle'; width: number; height: number }.

@ahejlsberg
Copy link
Member

Duplicate of #9246. This is working as intended because of #8548, but it is obviously causing some confusion.

@ahejlsberg ahejlsberg added the Duplicate An existing issue was already created label Jun 19, 2016
@yortus
Copy link
Contributor

yortus commented Jun 20, 2016

@ahejlsberg how do the conditions of #8548 come up with this example? The control flow type is (or should be?) narrowed to never after the two type guards in the if/else if block. But then there is no third type guard that conflicts with that narrowed type, so why should #8548 apply?

@ahejlsberg
Copy link
Member

Fixed in #10118.

@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

No branches or pull requests

3 participants