小编Але*_*нко的帖子

如何处理 ts 错误 - 目标需要 2 个元素,但源可能更少

我是TS新手。我不明白为什么 TS 认为Object.values(keyCodeToAxis[keyCode])可以返回一个长度小于 2 个元素的数组。

type ControlKey = "KeyQ" | "KeyW" | "KeyE" | "KeyA" | "KeyS" | "KeyD";
type Axis = "x" | "y" | "z";
interface AxesForKey {
  unchangingAxis: Axis;
  increasingAxis: Axis;
}

const keyCodeToAxis: Record<ControlKey, AxesForKey> = {
  "KeyQ": {
    unchangingAxis: "z",
    increasingAxis: "y",
  },
  "KeyW": {
    unchangingAxis: "x",
    increasingAxis: "y",
  },
  "KeyE": {
    unchangingAxis: "y",
    increasingAxis: "x",
  },
  "KeyA": {
    unchangingAxis: "y",
    increasingAxis: "z",
  },
  "KeyS": {
    unchangingAxis: "x",
    increasingAxis: "z",
  },
  "KeyD": { …
Run Code Online (Sandbox Code Playgroud)

typescript

10
推荐指数
1
解决办法
1万
查看次数

标签 统计

typescript ×1