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

[Requested Feature] Intersection types for enums #20136

Closed
Parasrah opened this issue Nov 18, 2017 · 4 comments
Closed

[Requested Feature] Intersection types for enums #20136

Parasrah opened this issue Nov 18, 2017 · 4 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@Parasrah
Copy link

TypeScript Version: 2.6.1

Code

enum Fruit {
  APPLE = 'Apple',
}

enum Veggies {
  CARROT = 'Carrot',
}

type FruitAndVeggies
  = Fruit
  & Veggies;

Requested behavior:
An intersection type of Fruit & Veggies
ie the following:

type FruitAndVeggies = 'Apple' & 'Carrot';

Actual behavior:

type FruitAndVeggies = never

Use Case
Writing a Vue application with Typescript and using Vuex. I have used enums to enforce type strictness on the names of the mutations, actions and getters similar to the following:

export enum MutationTypes {
  SET_DRAWER_SHOWN = 'setDrawerShown',
}

const mutations = {
  [MutationTypes.SET_DRAWER_SHOWN] (state: AppState) {
    // ...
  },
};

Doing it this way however prevents me from using the Mutations = Module1Mutations | Module2Mutations type as an argument to commit mutations, if one of my <module>Mutations enums is empty (due to commit(type: string). I have created a workaround, but this feature would be nice if other people are also interested in it. Also if anyone has an appropriate solution that I haven't though of suggestions are welcome 😁

Thanks for Typescript!

@ghost
Copy link

ghost commented Nov 19, 2017

'Apple' & 'Carrot' is indeed never because no string can be equal to 'Apple' but also be equal to 'Carrot'. Could you provide a simpler example of what you're trying to do?

@ghost ghost added the Working as Intended The behavior described is the intended behavior; this is not a bug label Nov 19, 2017
@Parasrah
Copy link
Author

Of course, having it explained that way makes my question seem dumb haha. Basically I have a situation like the following:

  • 2 enums with string values
  • A union type of both, which is essentially a union type of all the possible string values (correct me if I'm wrong)

The second that one of the enums is empty, the union type is no longer a union of strings, and therefore cannot be the parameter for a function requiring a string argument.

@ghost
Copy link

ghost commented Nov 19, 2017

You can union string enums -- that uses the | type operator instead of &. But if an enum is empty we treat it as a number enum and not a string enum.

@Parasrah
Copy link
Author

Alright, will just continue with the current workaround, thanks!

@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

1 participant