My classes are not specific and they look like this:
type SyncReducerAction<TState> = (state: TState, ...args: any[]) => TState;
type AsyncReducerAction<TState, TResult, TRest extends any[]> = {
promise: (...args: TRest) => Promise<TResult>;
pending?: (state: TState, ...args: TRest) => TState;
fullfilled: (state: TState, result: TResult, ...args: TRest) => TState;
rejected: (state: TState, error: Error, ...args: TRest) => TState;
};
I am trying to create a JSON object where the value can be one of these two types. Here is a function that should accept this object:
function get<
T extends {
[K in keyof T]: T[keyof T] extends SyncReducerAction<any>
? SyncReducerAction<Parameters<T[keyof T]>[0]>
: T[keyof T] extends AsyncReducerAction<
infer TState,
infer TResult,
infer TRest
>
? AsyncReducerAction<TState, TResult, TRest>
: never;
}
>(input: T): ReducersToActions<T> {
throw new Error("Not implemented");
}
However, this does not seem to work. Using only one type works fine.
Edit:
This version almost gets it right, but the types are still incorrect: Playground link. Removing TResult
from AsyncReducerAction
and setting it to any
seems to make it work.