Course contentsShow
← TypeScript
Lesson 110 of 1,7963. Basic TypesFree lesson

Multidimensional Arrays

Typing nested arrays with T[][] syntax and understanding type inference for nested structures.

Multidimensional Arrays

You'll learn how to type arrays that contain other arrays, like grids or tables.

What Are Multidimensional Arrays?

A multidimensional array is simply an array of arrays. Think of it like a spreadsheet: instead of a single column of values, you have rows and columns. In TypeScript, you can type these nested structures by adding extra brackets to your type annotation.

The Double-Bracket Syntax

To type an array of arrays, use T[][] where T is the inner element type:

const grid: number[][] = [
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9]
];

const names: string[][] = [
  ["Alice", "Bob"],
  ["Charlie", "Diana"]
];

The first [] means "array of," and the second [] means "arrays." So number[][] reads as "array of arrays of numbers."

Type Inference Still Works

TypeScript can infer multidimensional array types automatically:

const ticTacToe = [
  ["X", "O", "X"],
  ["O", "X", "O"],
  ["X", "X", "O"]
]; // Inferred as string[][]

const matrix = [
  [1, 2],
  [3, 4]
]; // Inferred as number[][]

Going Deeper

You can nest arrays as deeply as needed: number[][][] would be an array of arrays of arrays of numbers, though this becomes rare in practice.

const cube: number[][][] = [
  [[1, 2], [3, 4]],
  [[5, 6], [7, 8]]
];

Key Takeaway: Use T[][] syntax to type arrays containing arrays, and TypeScript will infer the nested structure when you initialize with nested array literals.