HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to TypeScriptBasic TypesUnion & Literal TypesInterfacesTyped FunctionsArrays & TuplesGenerics & Enums
REFERENCE
📖 Overview
🖥️ Basics & Output
🏷️ Types
📐 Interface & Type Alias
🧩 Function
📋 Array & Object Types
🚀 Next
TypeScript›Arrays & Tuples

Arrays & Tuples

Typed arrays (type[]), fixed-structure tuples (tuple), and dictionaries with Record.

Simple enough for a 5-year-old to understand

Tuple (tuple)

A tuple is a fixed array where the type and order of each position are specified.

Example code
1
const person: [string, number] = ["Alex", 36];
1st string, 2nd number — fixed.
Data1st string, 2nd number — fixed.
2
console.log(person[0], person[1]);
Alex 36
Output / InputAlex 36
DataOutput / Input
💡 Hint: The order and type in a tuple are fixed: ["Alex", 36] works, [36, "Alex"] gives an ERROR.

About this topic

In TypeScript, the array type declares the type of elements it contains. There are two writing styles — bracketed and generic — and both mean the exact same thing. The bracketed style is more common.

A tuple is a different structure: an array with a FIXED length and a SEPARATE type for each position. It's suitable for a coordinate pair, a key-value pair, or multiple results returned by a function. React's state hook returns exactly a tuple.

An important pitfall: TypeScript doesn't assume that array access can return undefined. When you read the fifth element from an array that doesn't have a fifth element, the type looks correct but the value is undefined. This behavior is fixed with the noUncheckedIndexedAccess setting.

What you will learn

  • Two ways to declare an array type
  • Read-only arrays
  • Defining and naming a tuple type
  • The difference between a tuple and an array
  • The undefined risk of index access
  • Type narrowing with const

When to use?

A tuple is a practical way to return two related values from a function. However, once you exceed three values, an object with named fields immediately becomes more readable — tuple positions are unnamed.

A read-only array is a way to document that a function will not modify the array it receives. Telling the caller through the type that you won't touch their array is stronger than saying it in a comment.

Common mistakes

Wrong

const point: [number, number] = [1, 2];
point.push(3);
console.log(point[2]);

Although the tuple length is fixed, the push method is not prevented — this is a known quirk of TypeScript's tuple model. However, trying to read the third element results in an error because the type only defines two positions. A readonly tuple prevents push as well.

Correct

const point: readonly [number, number] = [1, 2];
console.log(point[1]);

Wrong

const items = ["a", "b"];
const first: string = items[5];
console.log(first.length);

TypeScript does not assume that array access can return undefined; the type appears as string but the value is undefined, and at runtime you get a "Cannot read properties of undefined" error. The noUncheckedIndexedAccess setting turns this into a compile-time error.

Correct

const items = ["a", "b"];
const first = items[5];
if (first !== undefined) {
  console.log(first.length);
}

Wrong

function makePair(a: string, b: string) {
  return [a, b];
}
const [x, y]: [string, string] = makePair("a", "b");

TypeScript infers the return as a string array rather than a tuple; assigning it to a tuple throws a "Target requires 2 element(s) but source may have fewer" error. If you want a tuple, you must explicitly declare the return type or use as const.

Correct

function makePair(a: string, b: string): [string, string] {
  return [a, b];
}
const [x, y] = makePair("a", "b");

Frequently asked questions

Should I use a tuple or an object?

Tuples are practical up to two values, especially when the meanings of the positions are obvious. After three values, use an object: tuple positions are nameless, and the reader has to guess what each position represents.

What does as const do?

It locks a value to its narrowest possible type: arrays become tuples, strings become literal values, and everything becomes read-only. This is very useful for constant configuration objects and option lists, saving you from manually writing literal unions.

Is it worth enabling noUncheckedIndexedAccess?

Yes, but there is a cost: you have to write an undefined check for every array access, which makes the code longer. In return, it shifts JavaScript's most common runtime error to compile time. Enabling it by default in new projects is the right choice.

Summary

The array type specifies the element type, while a tuple is a fixed-length structure with types per position. TypeScript does not assume that array index access can return undefined — this is an important gap fixed by noUncheckedIndexedAccess.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code