将rootreducer传递给configureStore方法会使编译失败

时间:2018-08-17 18:32:20

标签: angular typescript redux

工具链

  • @ angular / cli

环境

  • NodeJS版本:8.11.3
  • 打字稿版本:2.9.3
  • Angular版本:6.1.3
  • @ angular-redux /商店版本:^ 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()] : []);

它应该编译并运行。

实际行为:

不会编译错误,指出减速器类型错误。注意,由于某种原因,它似乎丢失了ClientDataStore类型。因为我要在整个状态中组合多个reducer,所以似乎会松开中间接口的定义。

堆栈跟踪/错误消息:

src / app / classification / classification.component.ts(14,18)中的错误:错误TS2339:类型“ IAppStateClassfn”上不存在属性“实体”。 src / app / store / store.module.ts(33,13):错误TS2345:类型'Reducer << strong> {实体的参数:{类别:任何;属性:任何; }; classfnAppState:IClassfnAppState; },...'不可分配给'Reducer'类型的参数。   参数“状态”和“状态”的类型不兼容。     类型“ IAppState”不可分配给类型“ {实体:{类别:任何;属性:任何; }; classfnAppState:IClassfnAppState; }'。       属性“实体”在类型“ IAppState”中是可选的,但在类型“ {实体:{类别:任意;属性:任何; }; classfnAppState:IClassfnAppState; }'。

附加说明:

(可选)

1 个答案:

答案 0 :(得分:0)

当然,一旦我发布了这篇文章,我就发现了“问题”。该问题与错误无关。似乎编译器对一些混乱的旧接口定义感到困惑。我删除了接口,它编译良好。