Array.fill expects 1-3 arguments, got 0?

Viewed 1174

I'm migrating my react app to typescript and am running into an issue with these blocks of code:

const weekNumber = [
    ...Array(CURRENT_WEEK_NUMBER)
      .fill()
      .map((_, i) => i + 1)
  ];
  const weekLabels = [
    ...Array(17)
      .fill()
      .map((_, i) => i + 1),
    'Playoffs: Wild Card',
    'Playoffs: Divisional Round',
    'Playoffs: Conference Championship',
    'Playoffs: Super Bowl'
  ];

Expected 1-3 arguments, but got 0. An Argument for value was not supplied

I understand what this is telling me, but how do I refactor this code or have compiler ignore this?

1 Answers

Array.fill takes a value that you want to fill your Array with. By leaving out the parameter, it will be undefined at runtime, so to satisfy the compiler and keep the functionality the same, you would need to explicitly pass undefined:

Array(17)
    .fill(undefined)
    .map((_, i) => i + 1)

An alternative solution would be to use Array.from instead, something like:

Array.from({ length: 17 }, (_, i) => i +1)
Related