I defined a union type for actions in TypeScript:
type Action = {
type: 'reset',
} | {
type: 'add',
payload: number
} | {
type: 'minus',
payload: number
}
In some functions, I want to reference to a part of the union type, like:
function handleAdd(state: State, action: {
type: 'add',
payload: number
}): State => {
// handle the add
}
Is there a way to simplify the typing of action so I don't need to repeat the full definition of
{
type: 'add',
payload: number
}
here?
Update:
I don't want to predefine each action type separately. Is there another solution, like Action[type='add']?