I'm working with an array of arrays, for example:
const input = [['A', 'X'], ['B', 'Y'],...];
In addition to that, I have two enums:
enum MyMove {
Rock = 'X',
Paper = 'Y',
Scissors = 'Z',
};
enum OpponentMove {
Rock = 'A',
Paper = 'B',
Scissors = 'C',
};
I am trying to use Array.prototype.reduce()
on my input
array, but I'm struggling with annotating the callbackFn.currentValue
. This is what I have so far:
const score: number = input.reduce(
(acc: number, ([opponentMove, myMove]: [OpponentMove, MyMove])) => {
(referenced from Types when destructuring arrays), but I keep getting these errors:
02/index.ts:38:9 - error TS2322: Type 'string[]' is not assignable to type 'number'.
38 const score: number = input.reduce(
~~~~~
02/index.ts:39:21 - error TS2304: Cannot find name 'opponentMove'.
39 (acc: number, ([opponentMove, myMove]: [OpponentMove, MyMove])) => {
~~~~~~~~~~~~
02/index.ts:39:35 - error TS2304: Cannot find name 'myMove'.
39 (acc: number, ([opponentMove, myMove]: [OpponentMove, MyMove])) => {
~~~~~~
02/index.ts:39:44 - error TS2554: Expected 1-2 arguments, but got 3.
39 (acc: number, ([opponentMove, myMove]: [OpponentMove, MyMove])) => {
~~~~~~~~~~~~~~~~~~~~~~
What is the correct way to annotate [opponentMove, myMove]
in this case?