我在React-Redux应用程序中使用打字稿进行静态打字。我有一个减速器,就像这样:
export default function (state = { byContextId: {} }, action) {
const
{ contextId } = action,
contextState = _get(state, `byContextId.${contextId}`, DEFAULT_STATE);
let nextContextState;
switch (action.type) {
case FETCHING_CONTENT:
nextContextState = {
...contextState,
loading: true,
};
break;
case CONTENT_FETCHED:
nextContextState = {
...contextState,
loading: false,
};
break;
default:
return state;
}
return update(state, {
byContextId: {
$merge: {
[contextId]: nextContextState,
},
},
});
}
我有一个用于我的应用的选项卡式系统。因此,可以在多个上下文中打开同一组件,我将存储每个标签byContextId
的数据。因此,商店看起来像:
app
- myModule
-{someContextId} - myState
-{otherContextId} - myState
...
如果它是一个JS文件但带有打字稿,那么reducer的这段代码可以很好地工作,但给我错误。
ERROR in [at-loader] ./src/app/modules/ugc/reducers/homePageReducer.ts:40:5
TS2345: Argument of type '{ byContextId: { $merge: { [x: number]: any; }; }; }' is not assignable to parameter of type 'Spec<{ byContextId: {}; }, never>'.
Object literal may only specify known properties, and 'byContextId' does not exist in type 'Spec<{ byContextId: {}; }, never>'.
ERROR in [at-loader] ../../node_modules/immutability-helper/index.d.ts:41:42
TS2304: Cannot find name 'ReadonlyMap'.
我正在将amazing-typescript-loader
和immutability-helper
一起使用。