Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 676
Matthew Spahr Avatar asked Mar 09 '26 22:03

Matthew Spahr


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)
like image 141
TkDodo Avatar answered Mar 12 '26 14:03

TkDodo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!