英文:
Property does not exist on type (2339)
问题
我遇到了 TypeScript 错误 Property 'UPDATE_COLUMNS' does not exist on type 'DimensionAction'.ts(2339)
即使你可以看到它在上面被定义了。
我在使用 VSCode。我想知道这是否是我的 IDE 的一个 bug。我已经运行了 yarn lint
,代码中其他地方的语法都完全正常。
export enum DimensionAction {
UPDATE_ROWS = 'update_rows',
UPDATE_COLUMNS = 'update_columns',
}
const dimensionReducer = (state: Dimension, action: DimensionAction): void => {
switch (action) {
case DimensionAction.UPDATE_COLUMNS:
break;
case DimensionAction.UPDATE_ROWS:
break;
default:
throw Error('Must pass action');
}
}
为什么这段代码不是有效的 TypeScript 代码?
英文:
I'm getting the typescript error Property 'UPDATE_COLUMNS' does not exist on type 'DimensionAction'.ts(2339)
Even though you can see it is defined right above.
I'm using VSCode. I'm wondering if this is a bug with my IDE. I've run yarn lint
and the syntax everywhere else in my code is totally fine.
export enum DimensionAction {
UPDATE_ROWS = 'update_rows',
UPDATE_COLUMNS = 'update_columns',
}
const dimensionReducer = (state: Dimension, action: DimensionAction): void => {
switch (action) {
case action.UPDATE_COLUMNS:
break
case action.UPDATE_ROWS:
break
default:
throw Error('Must pass action')
}
}
Why isn't this code valid Typescript?
答案1
得分: 1
你应该在枚举类型本身上访问特定的枚举。
英文:
You should access the specific enumeration on the enum type itself.
case DimensionAction.UPDATE_COLUMNS:
Also, there is no need for a separate case for UPDATE_ROWS
; it can just be handled in default
. action: DimensionAction
does not allow for action
to be null
, undefined
, or anything other than one of the DimensionAction
enum values.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论