具有枚举类型问题的 Ngrx 操作
Ngrx actions with enum types issue
嘿,我正在尝试构建 actions/reducers/etc 但我 运行 遇到了一个令人沮丧的 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,
};
我目前使用的是:
- 打字稿 2.6.2
- Ngrx 4.1.1
这是一个经过精简的 repo: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
}
嘿,我正在尝试构建 actions/reducers/etc 但我 运行 遇到了一个令人沮丧的 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,
};
我目前使用的是:
- 打字稿 2.6.2
- Ngrx 4.1.1
这是一个经过精简的 repo: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
}