Currently, I am attempting to implement something similar to the following (although it is much more complex):
interface Base {
id: string;
info: object;
}
interface A extends Base {
example: boolean;
}
interface B extends Base {
max: number;
}
interface ActionDataTypes {
'for_a': A;
'for_b': B;
}
function tryAction<A extends keyof ActionDataTypes>(id: string, action: A, data: ActionDataTypes[A]): boolean {
switch(action){
case 'for_a':
//do stuff
break;
case 'for_b':
//do stuff
//this is a more complicated if statement
if(data.max > 12){
//do stuff
}
break;
}
}
An issue arises in TypeScript where the error message states
Property 'max' does not exist on type 'A | B'. Property 'max' does not exist on type 'A'.
I am seeking insight on how to resolve this error. Any ideas?
The following inquiries did not provide a solution:
- Typescript type safety in switch case statements
- Typescript does not consider the 'case' of a switch statement as a typeguard?
- Typescript is there a way to cast type for case in switch?
- TypeScript: Type guard does not work with switch statement when putting object into variable
- Why Typescript throw error message: Property does not exist on type?