0

I am running some code that operates perfectly in the browser, but VSCode underlines the fill() method in red, stating: Expected 1-3 arguments, but got 0.

Function:

const getRange = (start: number, end: number) => {
  return Array(end - start + 1)
    .fill()
    .map((v, i) => i + start)
}

const pagination = (currentPage: number, pageCount: number) => {
  let delta: number
  if (pageCount <= 7) {
    // delta === 7: [1 2 3 4 5 6 7]
    delta = 7
  } else {
    // delta === 2: [1 ... 4 5 6 ... 10]
    // delta === 4: [1 2 3 4 5 ... 10]
    delta = currentPage > 4 && currentPage < pageCount - 3 ? 2 : 4
  }

  const range = {
    start: Math.round(currentPage - delta / 2),
    end: Math.round(currentPage + delta / 2)
  }

  if (range.start - 1 === 1 || range.end + 1 === pageCount) {
    range.start += 1
    range.end += 1
  }

  let pages: any =
    currentPage > delta
      ? getRange(Math.min(range.start, pageCount - delta), Math.min(range.end, pageCount))
      : getRange(1, Math.min(pageCount, delta + 1))

  const withDots = (value, pair) => (pages.length + 1 !== pageCount ? pair : [value])

  if (pages[0] !== 1) {
    pages = withDots(1, [1, '...']).concat(pages)
  }

  if (pages[pages.length - 1] < pageCount) {
    pages = pages.concat(withDots(pageCount, ['...', pageCount]))
  }

  return pages
}

What is this method expecting, and what would be the best course of action to prevent VSCode flagging it as a problem please?

dom_ahdigital
  • 1,651
  • 18
  • 37
  • 1
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/fill - you should provide the value to fill with and optionally, the start/end index for what to be filled. – VLAZ Oct 22 '20 at 11:03
  • 1
    if you want to fill it with `undefined`s, typescript requires you to make that explicit, as in `.fill(undefined)` – Nicholas Tower Oct 22 '20 at 11:04
  • 1
    `getRange` should be `Array.from({length: end-start}, (_, i) => start + i)`. See also [Does JavaScript have a method like “range()” to generate a range within the supplied bounds?](https://stackoverflow.com/q/3895478) – VLAZ Oct 22 '20 at 11:04
  • Thanks @NicholasTower and VLAZ for the suggestions. – dom_ahdigital Oct 22 '20 at 11:07
  • https://www.w3schools.com/jsref/jsref_fill.asp worth a look – Rob Evans Oct 22 '20 at 11:35

0 Answers0