There is an enum for process. They have different steps, which are also represented as enums as shown below.
enum Process {
Simple = "simple",
Advanced = "advanced"
}
enum SimpleStep {
A = "A",
B = "B"
}
enum AdvancedStep {
A = "A",
B = "B",
C = "C"
}
With the following statements, I created an array of steps.
const SIMPLE_STEPS = Object.keys(SimpleStep).map(
(k: string) => SimpleStep[k]
);
const ADVANCED_STEPS = Object.keys(AdvancedStep).map(
k => AdvancedStep[k]
);
const ALL_STEPS = {
[Process.Simple]: SIMPLE_STEPS,
[Process.Advanced]: ADVANCED_STEPS
};
I wrote the following function to get the step number.
// ???: Check if S is a step of Process
const getStepNumber = <P extends Process, S>(process: P, step: S) => {
return ALL_STEPS[process].indexOf(step) + 1;
};
// returns 2, which is correct
console.log('step number of B', getStepNumber(Process.Advanced, AdvancedStep.B));
// returns 0. Is it possible to prevent at compile-time?
console.log('step number of C', getStepNumber(Process.Simple, AdvancedStep.C));
As you can see in the code example, is it possible to prevent calling the function with wrong step during compile-time using generics?
Here is the playground, if you wish to try out the whole example: TS Playground