I am trying to infer the data type in my doAction from the generic type of my GAction
I don't understand why I get the error message Argument of type 'string | number' is not assignable to parameter of type 'never'. Type 'string' is not assignable to type 'never'.
Please help me understand what I am doing wrong.
abstract class Action<GData> {
abstract isValid(data : GData): boolean
static dostuff<GData>(action: Action<GData>, data : GData): boolean {
return action.isValid(data);
}
}
abstract class State<GAction extends Action<any>> {
abstract doAction(action: GAction, data: any): State<GAction>
}
class NextAction extends Action<string> {
isValid(data: string): boolean {
return (data === "222")
}
}
class LastAction extends Action<number> {
isValid(data : number): boolean {
return (data === 254);
}
}
type AllStateActions = LastAction | NextAction
type ActionData<S> = S extends Action<infer H> ? H : never;
class OneState extends State<AllStateActions> {
doAction<GAction extends AllStateActions, GData extends ActionData<GAction>> (action: GAction , data: GData): OneState {
if(!action.isValid(data)) {
return this;
}
return this;
}
}