My code includes an enum that lists operations (which cannot be altered):
enum OpType {
OpA = 0,
OpB = 1,
}
In addition, there are object types defined to hold data required for each operation:
type A = {
readonly opType: OpType.OpA;
readonly foo: number;
};
type B = {
readonly opType: OpType.OpB;
readonly bar: string;
};
Furthermore, there is a handler function in place to ensure all operations are dealt with effectively:
type Ops = A | B;
export const ensureExhaustive = (_param: never) => {};
export const handleOp = (op: Ops) => {
switch (op.opType) {
case OpType.OpA:
if (op.foo < 80) { /* … */ }
break;
case OpType.OpB:
if (op.bar === 'foo') { /* … */ }
break;
default:
ensureExhaustive(op);
}
}
Despite the functionality of the handleOp
function, it only verifies handling of what is explicitly included in the Ops union – any new values added to the OpType
enum, like OpC = 2
, will not prompt detection that they are unhandled.
Is there a way to establish a "connection" between the enum values (potentially via an Ops
type) and the switch
statement within the handleOp
function to guarantee every value is addressed?