'How to specify the type of an array consisting of number and an array in typescript?
I have the following data structure which I want to pass as a parameter into a function.
let structure = [6, [197,47,28,191,198,129,117,82,171]]
It's given that the first element is always a number and the second element an array of numbers. When I want to pass it to my function, my IDE suggests following as input types:
function myFunc(structure:(number | number[])[]) {
....
}
This leads to problems because it is not specific enough. I know the first element is always a number, but based on my type declaration it doesn't have to be.
How do you declare the type here properly in typescript?
Solution 1:[1]
function myFunc(structure: [number, number[]]) {
....
}
This is called a tuple type.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Anastasia |
