cha*_*son 3 typescript ngrx ngrx-store
大家好,我正在尝试构建动作/减速器/等,但我遇到了令人沮丧的 TypeScript 问题。我正在尝试将字符串枚举用于操作类型,但无法使用以下内容进行编译:
这是代码:
import { Action, ActionReducerMap } from '@ngrx/store';
export enum AnnoSetsTypes {
ADD_MANY = '[AnnoSets] Add Many',
RESET = '[AnnoSets] Reset',
}
export class AddMany implements Action {
readonly type = AnnoSetsTypes.ADD_MANY;
constructor(public annoSets: any[]) { }
}
export class Reset implements Action {
readonly type = AnnoSetsTypes.RESET;
}
export type AnnoSetsAction = AddMany | Reset;
export interface IAnnoSetsState {
annoSets: any[];
}
export function annoSetsReducer(
state: IAnnoSetsState = { annoSets: [] },
action: AnnoSetsAction
): IAnnoSetsState {
switch (action.type) {
case AnnoSetsTypes.ADD_MANY:
return { annoSets: [...action.annoSets] };
case AnnoSetsTypes.RESET:
return { annoSets: [] };
default:
return state;
}
}
export interface State {
annoSets: IAnnoSetsState;
}
export const reducers: ActionReducerMap<State> = { // <-- COMPILE ERROR HERE
annoSets: annoSetsReducer,
};
Run Code Online (Sandbox Code Playgroud)
我目前正在使用以下内容:
这是一个带有修剪问题的回购:https : //github.com/cha55son/ngrx-action-type-issue
一些注意事项。我不想将枚举转换为字符串,因为它会导致 switch 语句中的类型缩小失败。此外,它似乎在这里工作得很好:https : //github.com/ngrx/platform/blob/master/example-app/app/core/actions/layout.ts#L3所以我假设有一些 TypeScript 配置我需要设置。据我所知,代码应该足够了。任何帮助,将不胜感激。谢谢!
在 中tsconfig.json,该设置strict: true也会设置strictFunctionTypes: true,从而导致错误。strictFunctionTypes明确禁用应该清除它。
{
"compilerOptions": {
"sourceMap": true,
"declaration": false,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"lib": [
"es2017",
"dom"
],
"target": "es5",
"module": "es2015",
"strict": true,
"strictFunctionTypes": false
},
"compileOnSave": true
}
| 归档时间: |
|
| 查看次数: |
708 次 |
| 最近记录: |