Search code examples
reactjstypescriptvisual-studio-codeenumsvscode-snippets

How to create a switch statement from enum type so it automatically fills case for each enum member?


This feature exists in Visual Studio Professional, but no idea how to do it in VS Code.

For example I have Colors enum like this:

enum Colors {
    Red,
    Blue,

So when I write a switch statement like this:

function getColor(colors: Colors) {
 switch (colors) 
}

The editor to automatically fill every case for me, like:

switch (colors) {
    case Colors.Red:      
      break;
    case Colors.Blue:      
      break;
}

Solution

  • There is extension that covers this case called Hocus Pocus:

    https://marketplace.visualstudio.com/items?itemName=nicoespeon.hocus-pocus

    When installed just hover over switch statement and open quick fix menu (ctrl + . or cmd + .) and You should see Create all cases option