【发布时间】:2018-08-17 18:32:20
【问题描述】:
工具链
- @angular/cli
环境
- NodeJS 版本:8.11.3
- 打字稿版本:2.9.3
- Angular 版本:6.1.3
- @angular-redux/store 版本:^9.0.0
- @angular/cli 版本:(如果适用):6.1.2
- 操作系统:Windows
预期行为:
我定义了以下类型和减速器(为了清楚起见,缩写)
export type LookupNum<T> = { [id: number]: T };
//-- Normalized model objects suitable for the store that use string keys --//
export interface NormalizedObjectsStr<T> {
byId: LookupNum<T>;
allIds: number[];
}
//-- Normalized model objects suitable for the store that use string keys --//
export interface NormalizedObjectsNum<T> {
byId: { [id: number]: T };
allIds: number[];
}
//-- Client side store of our data models --//
export interface ClientDataStore {
categories: NormalizedObjectsNum<Category>
attributes: NormalizedObjectsNum<Attribute>
}
// Definition of application state
export interface IAppState {
entities?: ClientDataStore;
classfnAppState?: IClassfnAppState;
}
//-- Entity reducers --//
const categoriesByIdReducer: Reducer<LookupNum<Category>, FluxStandardAction<Category | number>> = (state, action) => {
switch (action.type) {
default: { return state; }
}
}
const allCategoryIdsReducer: Reducer<number[], FluxStandardAction<Category | number>> = (state, action) => {
switch (action.type) {
default: {
return state;
}
}
}
const attributesByIdReducer: Reducer<LookupNum<Attribute>, FluxStandardAction<Category | number>> = (state, action) => {
switch (action.type) {
default: { return state; }
}
}
const allAttributesIdsReducer: Reducer<number[], FluxStandardAction<Attribute | number>> = (state, action) => {
switch (action.type) {
default: {
return state;
}
}
}
const categoriesReducer = combineReducers({
byId: categoriesByIdReducer,
allIds: allCategoryIdsReducer
});
const attributesReducer = combineReducers({
});
const entityReducer = combineReducers({
categories: categoriesReducer,
attributes: attributesReducer
});
//-- Create Root Reducer --//
export default combineReducers({
entities: entityReducer,
classfnAppState: classfnRootReducer
});
当我使用此调用配置商店时:
import appRootReducer from './reducers';
store.configureStore(
appRootReducer,
INITIAL_STATE,
[createLogger()],
devTools.isEnabled() ? [devTools.enhancer()] : []);
它应该编译并运行。
实际行为:
它不会编译并显示reducer 类型错误的错误。请注意,由于某种原因,它似乎丢失了 ClientDataStore 类型。因为我在整个状态中组合了多个减速器,所以它似乎松散了中间接口定义。
堆栈跟踪/错误消息:
src/app/classification/classification.component.ts(14,18) 中的错误:错误 TS2339:“IAppStateClassfn”类型上不存在属性“实体”。 src/app/store/store.module.ts(33,13): error TS2345: Argument of type 'Reducer{ entity: { categories: any;属性:任何; }; classfnAppState: IClassfnAppState; }, ...' 不能分配给“Reducer”类型的参数。 参数 'state' 和 'state' 的类型不兼容。 类型“IAppState”不可分配给类型“{实体:{类别:任何;属性:任何; }; classfnAppState:IClassfnAppState; }'。 属性“实体”在“IAppState”类型中是可选的,但在“{实体:{类别:任何;类型”中是必需的;属性:任何; }; classfnAppState:IClassfnAppState; }'。
补充说明:
(可选)
【问题讨论】:
标签: angular typescript redux