export interface State {
orderids: string[];
entities: { [orderID: string]: OrderHead };
selectedOhID: string | null;
};
// Set initial state to empty
const initialState: State = {
orderids : [],
entities: {},
selectedOhID: null,
};
export function OHreducer(state = initialState, action: orderhead_imp.Actions): State{
switch(action.type){
case orderhead_imp.ActionTypes.CREATE_OH:
let orders = action.payload;
let newOrders = orders.filter(orderhead => !state.entities[orderhead.orderID]);
let newOrderIds = newOrders.map(orderhead => orderhead.orderID);
let newOrderHeadEntities = newOrders.reduce((entities: { [orderID: string]: OrderHead }, orderhead: OrderHead) => {
return Object.assign(entities, {
[orderhead.orderID]: orderhead
});
}, {});
return {
orderids: [ ...state.orderids, ...newOrderIds ],
entities: Object.assign({}, state.entities, newOrderHeadEntities),
selectedOhID: state.selectedOhID
};
default:
return state;
};
}
나는 다른 작업을 소개하는 경우는, 그러나, 잘 작동으로
export const ActionTypes = {
CREATE_OH: type('[ORDERHEAD] Create Orderhead'),
MODIFY_SELECTED_OH: type('[ORDERHEAD] Select Orderhead'),
};
export class CreateOHAction implements Action {
type = ActionTypes.CREATE_OH
constructor(public payload: OrderHead[]) { }
}
export type Actions
= CreateOHAction
| SelectOHAction;
을
export class SelectOHAction implements Action {
type = ActionTypes.MODIFY_SELECTED_OH
constructor(public payload: string) { }
}
알림 :이 작업의 페이로드는 은 곧이 저장, 또는 컴파일 시도로, 타이프 라이터 지금 상태 : "필터는 입력 문자열에 exisit하지 않습니다 | OrderHead []"이제
내 감속기에 가서 새로운 케이스 추가하는 경우 :
을 의 매핑 action.payload 때case orderhead_imp.ActionTypes.MODIFY_SELECTED_OH: {
return {
orderids: state.orderids,
entities: state.entities,
selectedOhID: action.payload
};
}
내가 타이프 라이터 오류를 얻을 :
는 TS 오류 를 throw |
O "를 문자열 OrderHead는 [] 문자열을 입력하기 위해 할당 할 수없는" 분명히 두 경우 모두 페이로드에는 다른 데이터 구조가 있습니다. 각 경우가 action.payload에 대한 올바른 유형을 선택하도록 내 코드를 다른 방식으로 변경해야합니까?
업데이트
그래서 내 행동에 나는 같은 페이로드를 정의 할 경우 대신 "문자열"은 그러나이 매우 해키 (그리고 예상되지 것, 문제없이 컴파일하고 작동하는 것 같다의 "모든" 동작)
export class SelectOHAction implements Action {
type = ActionTypes.MODIFY_SELECTED_OH
constructor(public payload: any) { }
}
상태에서'selectedOhID : string | null'을'selectedOhID : string'으로 시도하십시오. –
아니요, 먼저 시도해 봤지만 어떤 것이 든 내 감속기에 유형을 설정하면 selectedOhID에 대해서는 여전히 실패합니다. 유형 검사 오류는 작업에 정의 된 페이로드 값에서 오류를 확인하고 감속기 유형 정의에서 오류가 아닙니다. – crooksey
어떤 타이프 스크립트 버전을 사용하고 있습니까? 문자열 리터럴 (그러므로 차별화 된 유니온)에 영향을 미치는 typescript 2.1+에서 급격한 변화 (틀림없이 버그 수정)가있었습니다. – rob3c