export interface IState {
name: string;
age: number;
}
export type IStateAction =
| {
type: "update_name";
value: string;
}
| {
type: "reduce_age";
};
const reducer: Reducer<IState, IStateAction> = (state, action) => {
switch (action.type) {
case "update_name":
return { ...state, name: action.value };
case "reduce_age":
return { ...state, age: --state.age };
default:
return state;
}
};
const init = (initState?: Partial<IState>): IState => {
if (!initState) {
return initValue;
} else {
return { ...initValue, ...initState };
}
};