Typescript: Provide a quick fix to add missing enum member

Created on 14 Aug 2020  ·  4Comments  ·  Source: microsoft/TypeScript

enum TokenKind {
  LeftBrace,
}

function scan(text: string, pos: number) {
  let c = text[pos];
  switch (c) {
    case '{': return TokenKind.LeftBrace;
    case '}': return TokenKind.RightBrace;
  }
}

We should have a quick fix for the error message

Property 'RightBrace' does not exist on type 'typeof TokenKind'.

to add RightBrace as a missing enum member. The resulting code should look like

enum TokenKind {
  LeftBrace,
  RightBrace,
}

function scan(text: string, pos: number) {
  let c = text[pos];
  switch (c) {
    case '{': return TokenKind.LeftBrace;
    case '}': return TokenKind.RightBrace;
  }
}
Quick Fixes Suggestion good first issue help wanted

Most helpful comment

Err, @DanielRosenwasser this already exists, I think. Copy & pasting that sample into the playground does give a quick fix.

Playground Link: Provided

All 4 comments

One could probably fold in the logic into the existing "add missing member" quick fix, which would be preferable.

"What happens if you have something that's both a class and enum?"

I don't really know. Don't offer the quick fix in that case.

Err, @DanielRosenwasser this already exists, I think. Copy & pasting that sample into the playground does give a quick fix.

Playground Link: Provided

Was this page helpful?
0 / 5 - 0 ratings