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

Generics do not preserve scoped union type of enums #24919

Closed
trshafer opened this issue Jun 13, 2018 · 3 comments
Closed

Generics do not preserve scoped union type of enums #24919

trshafer opened this issue Jun 13, 2018 · 3 comments
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@trshafer
Copy link

trshafer commented Jun 13, 2018

TypeScript Version: 3.0.0-dev.201xxxxx
I was unable to try this. This is attempted on current playground version (2.8 or 2.9, the playground does not specify)

Search Terms: NonNullable, union

Code

declare function test<T>(condition: T): NonNullable<T>;

enum A {
  ONE,
  TWO,
}

function thing(x: A.ONE) { // or a union type of some of the enum values.
  if (x === A.TWO) {
    // x cannot be A.TWO
  }

  const y = test(x);
  
  if (y === A.TWO) {
    // y can be A.TWO....
  }
}

Expected behavior:
y should not be assignable to A.TWO. y should remain A.ONE type.

Actual behavior:
y becomes type A instead of incoming A.ONE type.

Playground Link:
https://www.typescriptlang.org/play/#src=declare%20function%20test%3CT%3E(condition%3A%20T)%3A%20NonNullable%3CT%3E%3B%0D%0A%0D%0Aenum%20A%20%7B%0D%0A%20%20ONE%2C%0D%0A%20%20TWO%2C%0D%0A%7D%0D%0A%0D%0Afunction%20thing(x%3A%20A.ONE)%20%7B%0D%0A%20%20if%20(x%20%3D%3D%3D%20A.TWO)%20%7B%0D%0A%20%20%20%20%2F%2F%20x%20cannot%20be%20A.TWO%0D%0A%20%20%7D%0D%0A%20%20const%20y%20%3D%20test(x)%3B%0D%0A%20%20%0D%0A%20%20if%20(y%20%3D%3D%3D%20A.TWO)%20%7B%0D%0A%20%20%20%20%2F%2F%20y%20can%20be%20A.TWO....%0D%0A%20%20%7D%0D%0A%7D%0D%0A

Related Issues: #23884 #23046

Thanks for taking a look!

@mhegazy
Copy link
Contributor

mhegazy commented Jun 13, 2018

Literal types are not inferred unless there is a "marker" for the compiler e.g. constraint that suggests literal types are allowed.. so the type of y is A and not A.One.

you can get the desired behavior by defining test as:

declare function test<T extends number>(condition: T): NonNullable<T>;

@mhegazy mhegazy added the Design Limitation Constraints of the existing architecture prevent this from being fixed label Jun 13, 2018
@trshafer
Copy link
Author

I realized this is unreleated to NonNullable and only relates to generics:

enum B {
  ONE,
  TWO
}

class A1<T> {
  constructor(readonly x: T) {}
}

class A2<T extends number> {
  constructor(readonly x: T) {}
}

console.log(new A1(B.ONE).x === B.TWO); // OK
console.log(new A2(B.ONE).x === B.TWO); // Error

Play link

@trshafer trshafer changed the title NonNullable does not preserve scoped union type of enums Generics do not preserve scoped union type of enums Jun 15, 2018
@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed
Projects
None yet
Development

No branches or pull requests

3 participants