I have an array of arrays (e.g.
const input = [['A', 'X'], ['B', 'Y'],...];
). I also have two enum
s:
enum MyMove {
Rock = 'X',
Paper = 'Y',
Scissors = 'Z',
};
enum OpponentMove {
Rock = 'A',
Paper = 'B',
Scissors = 'C',
};
I want to run a Array.prototype.reduce()
on input
, but I can't figure out how to annotate callbackFn.currentValue
. I want to do something like this:
const score: number = input.reduce(
(acc: number, ([opponentMove, myMove]: [OpponentMove, MyMove])) => {
(based on Types when destructuring arrays), but I'm getting the following 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 proper way to annotate [opponentMove, myMove]
?