Question: I have some code:

```
enum Color {
    Red,
    Green,
    Blue
}

function getColorName(c: Color): string {
    switch(c) {
        case Color.Red:
            return 'red';
        case Color.Green:
            return 'green';
        // Forgot about Blue
    }

    throw new Error('Did not expect to be here');
}

```

I forgot to handle the `Color.Blue` case and I'd prefer to have gotten a compile error. How can I structure my code such that TypeScript flags this as an error?

Answer: 
The easiest way to find a missing case is to activate TypeScript's check for [no implicit returns](https://www.typescriptlang.org/tsconfig#noImplicitReturns). Just set `noImplicitReturns` to `true` in the `compilerOptions` section of your `tsconfig.json` file.

Afterwards you have to remove the `throw new Error` statement from your code, because it will prevent the TypeScript compiler from throwing an error (because your code is already throwing an error):

```
enum Color {
  Red,
  Green,
  Blue
}

function getColorName(c: Color): string {
  switch (c) {
    case Color.Red:
      return 'red';
    case Color.Green:
      return 'green';
  }
}

```

With the above code, you will have an implicit return (because if no case matches, the function will return `undefined`) and TypeScript's compiler will throw an error:

> 
> TS2366: Function lacks ending return statement and return type does not include 'undefined'.
> 
> 
> 

I've also made a video which demonstrates it: <https://www.youtube.com/watch?v=8N_P-l5Kukk>

In addition, I suggest to narrow down the return type of your function. It actually cannot return any `string` but only a defined set of strings:

```
function getColorName(c: Color): 'red' | 'blue'

```

Narrowing your return type can also help you to find missing cases as some IDEs (like VS Code & WebStorm) will show you when you have unused fields.
Create a custom function instead of using a `switch` statement.

```
export function exhaustSwitch<T extends string, TRet>(
  value: T,
  map: { [x in T]: () => TRet }
): TRet {
  return map[value]();
}

```

Example usage

```
type MyEnum = 'a' | 'b' | 'c';

const v = 'a' as MyEnum;

exhaustSwitch(v, {
  a: () => 1,
  b: () => 1,
  c: () => 1,
});

```

If you later add `d` to `MyEnum`, you will receive an error `Property 'd' is missing in type ...`