我是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 ×1