<code>type ActionType = ActionTypeA | ActionTypeB | ActionTypeC;
type ActionHandlerType<
PayloadType extends Object,
ActionParamType extends ActionType,
> = {
isTypeOfAction: (action: ActionType) => action is ActionParamType;
handleAction: (action?: ActionParamType, payload?: PayloadType) => void;
};
</code>
<code>type ActionType = ActionTypeA | ActionTypeB | ActionTypeC;
type ActionHandlerType<
PayloadType extends Object,
ActionParamType extends ActionType,
> = {
isTypeOfAction: (action: ActionType) => action is ActionParamType;
handleAction: (action?: ActionParamType, payload?: PayloadType) => void;
};
</code>
type ActionType = ActionTypeA | ActionTypeB | ActionTypeC;
type ActionHandlerType<
PayloadType extends Object,
ActionParamType extends ActionType,
> = {
isTypeOfAction: (action: ActionType) => action is ActionParamType;
handleAction: (action?: ActionParamType, payload?: PayloadType) => void;
};
Considering the above example. I want to restructure the ActionHandlerType
type in such a way that I don’t need to pass the second generic ActionParamType
and the ‘action’ parameter type in the handleAction
function should automatically infer from the return type (one of ActionType) from the isTypeOfAction
.
Something like..
<code>type PayloadType = { payload: 'test'}
// Not passing the second param.
export function someFunction(
): ActionHandlerType<PayloadType> {
return {
isTypeOfAction,
handleAction,
};
function isTypeOfAction(action: ActionType): action is ActionTypeA {
return action.type === 'A';
}
// It should automatically infer ActionTypeA from the isTypeOfAction function return type.
function handleAction(action: ActionTypeA, record: {}): void {
//..
}
}
</code>
<code>type PayloadType = { payload: 'test'}
// Not passing the second param.
export function someFunction(
): ActionHandlerType<PayloadType> {
return {
isTypeOfAction,
handleAction,
};
function isTypeOfAction(action: ActionType): action is ActionTypeA {
return action.type === 'A';
}
// It should automatically infer ActionTypeA from the isTypeOfAction function return type.
function handleAction(action: ActionTypeA, record: {}): void {
//..
}
}
</code>
type PayloadType = { payload: 'test'}
// Not passing the second param.
export function someFunction(
): ActionHandlerType<PayloadType> {
return {
isTypeOfAction,
handleAction,
};
function isTypeOfAction(action: ActionType): action is ActionTypeA {
return action.type === 'A';
}
// It should automatically infer ActionTypeA from the isTypeOfAction function return type.
function handleAction(action: ActionTypeA, record: {}): void {
//..
}
}
2