每当我array.map在元组上使用时,Typescript 都会将其推断为通用数组。例如,这里有一些简单的 3x3 数独游戏:
const _ = ' ' // a "Blank"
type Blank = typeof _
type Cell = number | Blank
type Three = [Cell, Cell, Cell]
type Board = [Three, Three, Three]
const initialBoard: Board = [
[_, 1, 3],
[3, _, 1],
[1, _, _],
]
// Adds a `2` to the first cell on the first row
function applyMove(board: Board): Board {
// errors here
const newBoard: Board = board.map((row: Three, index: number) …Run Code Online (Sandbox Code Playgroud)