I have the following types
export type Player = 'x' | 'o'
export type GameFieldValue = null | Player;
export type Board = [
[GameFieldValue, GameFieldValue, GameFieldValue],
[GameFieldValue, GameFieldValue, GameFieldValue],
[GameFieldValue, GameFieldValue, GameFieldValue],
]
Say I have an object of type Board
const board : Board = [
['x','x','o'],
['o','x','x'],
['x','x','x']
]
Now, I need a function that can invert all the values of the board (eg. 'x'
becomes 'o'
and the other way around)
const boardInverted : Board = [
['o','o','x'],
['x','o','o'],
['o','o','o']
]
The function I've come up with
function invertBoardValues(b:Board):Board {
return b.map(row => row.map(val => val === 'x' ? 'o' : 'x'))
}
However, I get the error
Type '("x" | "o")[][]' is not assignable to type 'Board'.
Target requires 3 element(s) but source may have fewer.
What do I do to fix this?