I have a union type defined as follows:
type Action = 'foo' | 'bar' | 'baz';
I would like the compiler to throw an error if someone attempts to extend this type.
Consider the code snippet below:
type Action = 'foo' | 'bar' | 'baz';
function create(action: Action) {
let something;
switch (action) {
case 'foo':
something = action.toLowerCase();
break;
case 'bar':
something = action.toUpperCase();
break;
}
return something;
}
In this example, the baz
case is not handled, yet TypeScript does not raise any errors. Is there a way to enforce this and make TypeScript flag such cases where types are extended without handling all possible options?
playground: here