[T, U] Tuple Type
Last updated: 2026-10-22
[T, U] Tuple Type
A tuple is an array type with fixed length and per-position types — ideal for coordinates, key-value pairs and multi-value returns where position carries meaning.
| Group | Basic Types |
|---|---|
| Kind | Type |
| Strict mode | Not required |
ℹ️ Info The editor transpiles TypeScript to JavaScript and runs it (no type checking); see each example's Output for type-check results.
📝 Syntax
type Point = [number, number];
type Named = [name: string, age: number]; // labeled tuple
let pair: Point = [3, 4];
⚙️ Parameters
| [T, U] | Positional types: index 0 is T, index 1 is U; length is fixed too |
|---|---|
| [lat: number] | [lat: number, lng: number] names each position, purely for readability |
Returns:None — tuples are type-level only; at runtime it is a plain array.
▶ Example
Edit the code below and click Try it to see the transpiled result:
Output:
rgb(255, 0, 0)
(Runtime output after transpilation; red.push("x") and red = [255, 0] are both compile errors)
💡 Notes
ℹ️ Info A tuple is a plain array at runtime; the length constraint lives only at compile time.
💡 Tip Labeled tuples like [lat: number, lng: number] improve destructuring and IDE hints — strongly recommended for multi-value returns.
⚠️ Note push can still append declared-type elements beyond the fixed length: [number, number].push(1) is allowed. Position semantics need discipline.
🏷️ Related Basic Types
💡 Related cases
More cases coming soon.
📚 Related tutorials
❓ FAQ
Q What is the difference between tuples and arrays?
A Arrays vary in length with one element type; tuples are fixed with per-position types. Use tuples for "each slot means something different" like [lng, lat], arrays for collections.
Q Can tuple elements be optional?
A Yes — [string, number?] means the second slot may be absent; optional elements must come last.
Q How do tuples relate to rest parameters?
A Rest parameters are tuples: function f(...args: [string, number]) precisely requires a string then a number.