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

Conditional type on enum is not inferring type in switch statement #31419

Closed
allevaton opened this issue May 16, 2019 · 2 comments
Closed

Conditional type on enum is not inferring type in switch statement #31419

allevaton opened this issue May 16, 2019 · 2 comments

Comments

@allevaton
Copy link

allevaton commented May 16, 2019

TypeScript Version: 3.5.0-dev.20190515

Search Terms: conditional type, enum, value, switch, inference

Code

I have a conditional type mapping an enum descriptor to real types:

export type PropertyDataToValue<T extends Forge.PropertyData>
  = T extends Forge.PropertyData.NONE ? null | undefined
  : T extends Forge.PropertyData.BoolProperty ? boolean
  : T extends Forge.PropertyData.FloatProperty ? number
  : T extends Forge.PropertyData.IntegerProperty ? number
  : T extends Forge.PropertyData.UnsignedIntegerProperty ? number
  : T extends Forge.PropertyData.StringProperty ? string
  : T extends Forge.PropertyData.Vector3Property ? Vector3
  : T extends Forge.PropertyData.Vector4Property ? Vector4
  : never;

Later, I want to switch on property data types, like so:

function valueToPropertyData<T extends Forge.PropertyData>(
  propertyType: T,
  value: PropertyDataToValue<T>,
) {
  switch (propertyType) {
    case Forge.PropertyData.StringProperty:
      // Here: `value` is of type PropertyDataToValue<T>
      return;
  }
}

Expected behavior:
In the "case", I'd expect value to be inferred as a string type. Similarly for each conditional type.

Actual behavior:

In the "case", value is of type PropertyDataToValue

Playground Link:

Playground URL

Related Issues:

I found this that seems related, but not quite: #23578

@jack-williams
Copy link
Collaborator

Duplicate of #24085

@allevaton
Copy link
Author

Duplicate of #24085

Thanks for finding that, I'll close this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants