属性在类型上不存在 (2339)

huangapple go评论78阅读模式
英文:

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.

属性在类型上不存在 (2339)

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.

huangapple
  • 本文由 发表于 2023年6月29日 11:39:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76577918.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定