即使指定了数组类型的打字稿,拼接显示从不

时间:2021-07-02 03:58:23

标签: arrays typescript generics interface vuex

我有一个包含数组的接口:

export interface AnimationCanvasState {
  frames: Array<Frame>
}

frames 数组 def 的类型为 Frame

我将此接口应用于 vuex 状态:

const state: AnimationCanvasState = {
  frames: [],
}


export const animationCanvas: Module<AnimationCanvasState, RootState> = {
  namespaced: true,
  state,
  getters,
  mutations
}

我的问题是当我尝试将数据拼接到数组中时:

export enum AnimationCanvasMutations {
  ADD_FRAME = "ADD_FRAME"
}

export const mutations: MutationTree<AnimationCanvasState> = {
  [AnimationCanvasMutations.ADD_FRAME] (state, index:number | undefined) {

        if (index !== undefined) {
            state.frames.splice(index, 0, new Frame(8, 8));
        } else {
            state.frames.push(new Frame(8, 8));
        }
  }
}

我收到以下错误: enter image description here

我查过这样的错误,所有的解决方案似乎都说“你需要输入你的数组”,但我做到了。我可以确认确实输入了 state.frames

enter image description here

enter image description here

我不明白为什么编译器会在接口中明确键入此数组类型时将其视为从不可见。有什么想法吗?

0 个答案:

没有答案