typescriptvariablestypestyped-arrays

How to create an array of strongly typed arrays?


I'm new in TS, but i didn't find answer for my question, so that's why I'm here)

I'm trying to create an array of arrays, that may contain only strongly typed data. For example like [[string, number], [string, number], [string, number], ...]

I tried to write such simple code:

let arr: Array<[string, number]> = [['dad\'s adidas', 123], ['moms gucci', 47]]

for (let i = 0; i < arr.length; i++) {
    for (let j = 0; i < arr[i].length; j++) {
        console.log(arr[i][j])
    }
    console.log('\n')
}

but the The compiler swears and give me this stupid error))

Uncaught SyntaxError C:\Users\sv25b\UbuntuProjects\WTF\main.ts:1
let arr: Array<[string, number]> = [['dad\'s adidas', 123], ['moms gucci', 47]]

What did I do wrong?


Solution

    1. run npm install tsx - install typescript runner
      • or install it globally with npm install --global tsx
    2. run npx tsx watch main.ts to run your file, in watch mode (restarting on file save)
    3. see that your code infinitly logs undefined
    4. fix i < arr[i].length to j < arr[i].length