[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.

GroupBasic Types
KindType
Strict modeNot 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.
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.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏