Fixed and labeled tuples
Concept and syntax
A tuple specifies element types by position. [name: string, count: number] expects a string at position 0 and a number at position 1. Labels document intent but are not runtime property names. Unlike (string | number)[], a fixed tuple describes known positions and length at the type level. Mutable tuple methods can still change runtime length, so do not mistake it for a runtime fixed-size container.
Working example
Compile standalone examples in a fresh directory with tsc --strict --target ES2022 --module commonjs --noEmitOnError main.ts, then node main.js.
const entry: [name: string, count: number] = ["tea", 3];const label: string = entry[0];const count: number = entry[1];console.log(label, count);Expected output (one line per log):
tea 3Common mistake
Intentionally invalid; check this snippet separately. Do not run it.
const entry: [name: string, count: number] = ["tea", "3"];Expected compiler diagnostic: TS2322. The second position requires number. Its label is documentation, not a conversion rule.
Practice and recap
Describe an inventory tuple with label and quantity. Try a reversed initializer and correct it without changing the contract.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Fixed and labeled tuples — knowledge check
Check the concept, apply it to a new choice, and interpret a compiler diagnostic.
What does [name: string, count: number] describe?
How do you read the count in this tuple?
Which initializer matches the tuple positions?
const entry: [name: string, count: number] = ["tea", "3"];