Skip to content

[Requested Feature] Intersection types for enums #20136

Closed
@Parasrah

Description

@Parasrah

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions