0

In Next.js I create a function component and use useState hook to declare variable for store array of digits like this

const [digits, setDigits] = useState<number[]>();

But I also want to define the range of array generic, something like Array(3) (but not [number, number, number] because that is too long)

How to solve this?

Artzeeker
  • 117
  • 10
  • 2
    Does this answer your question? [How to declare a Fixed length Array in TypeScript](https://stackoverflow.com/questions/41139763/how-to-declare-a-fixed-length-array-in-typescript) – ldruskis Feb 10 '22 at 14:30
  • @ldruskis So the answer is CAN'T, right? Only tuple is works. – Artzeeker Feb 10 '22 at 14:39

1 Answers1

0

You can do something like this:

const data = new Array<number>(100)

An array length of 100 where the elements have a type of number. Array comes with with a fill function to initialize your array if needed, for example

const array = new Array<number>(100).fill(9)

An array of length 100 where all indexes will be number 9.

dparr
  • 81
  • 3