, Elm lang Typescript:
,
interface ComponentState {
text: string
}
2 .
interface SetAction {
type: 'SET_VALUE', payload: string
}
interface ResetAction {
type: 'RESET_VALUE'
}
2 ( Typescript):
type ComponentAction = SetAction | ResetAction;
:
function componentReducer(state: ComponentState, action: ComponentAction): ComponentState {
}
, "" , :
interface ParentComponentState {
instance1: ComponentState,
instance2: ComponentState,
}
, Component, . , :
interface Instance1ParentAction {
type: 'INSTNACE_1_PARENT',
payload: ComponentAction,
}
interface Instance2ParentAction {
type: 'INSTNACE_2_PARENT',
payload: ComponentAction,
}
:
type ParentComponentAction = Instance1ParentAction | Instance2ParentAction;
- :
function parentComponentReducer(state: ParentComponentState, action: ParentComponentAction): ParentComponentState {
switch (action.type) {
case 'INSTNACE_1_PARENT':
return {
...state,
instance1: componentReducer(state.instance1, action.payload),
};
}
}
.