After conducting extensive research, I have yet to discover a definitive answer to this query.
There is a question posted on Stack Overflow that provides guidance on how to implement a thorough switch
statement:
How can I ensure my switch block covers all cases in TypeScript?
The suggested solution involves creating a function that accepts never
as an argument, returns never
, and triggers an error if invoked with any input.
function assertUnreachable(x: never): never {
throw new Error("Unexpected scenario");
}
This function should be utilized within the default
case of the switch
statement:
switch (action.type) {
case "A": {
...
}
case "B": {
...
}
default: {
return assertUnreachable(action);
}
}
However, utilizing this approach within a reducer
may pose challenges. Although it effectively ensures exhaustiveness for the reducer actions, it may result in errors at runtime when Redux internally triggers certain actions like:
@@redux/INITh.b.0.x.q.h
@@redux/PROBE_UNKNOWN_ACTIONe.7.b.o.p
Therefore, what is the most optimal method for handling exhaustiveness checks in a switch
statement within a reducer?