typescript
/

TypeScript Tuples

Last Sync: Today

On this page

10
0%
5 min read
Remaining
5 minleft

Click any section to jump — progress syncs automatically

typescript

TypeScript Tuples

What is a Tuple?

A tuple in TypeScript is a special type of array with a fixed number of elements and predefined types for each position.

Basic Example

TypeScriptRead-only
1
let user: [string, number] = ['John', 30];

Access Tuple Values

TypeScriptRead-only
1
console.log(user[0]); // John
console.log(user[1]); // 30

Named Tuples

TypeScriptRead-only
1
let user: [name: string, age: number] = ['John', 30];

Optional Tuple Elements

TypeScriptRead-only
1
let user: [string, number?] = ['John'];

Tuple with Rest

TypeScriptRead-only
1
let data: [string, ...number[]] = ['A', 1, 2, 3];

Tuple vs Array

FeatureTupleArray
LengthFixedDynamic
TypesFixed orderSame type
UsageStructured dataList of values

Best Practices

  • Use tuples for fixed structure data
  • Keep tuple size small
  • Use named tuples for readability
  • Avoid overly complex tuples

Common Mistakes

  • Using tuples like arrays
  • Ignoring tuple order
  • Adding extra elements incorrectly
  • Overusing tuples instead of objects

Conclusion

Tuples are useful for representing fixed, structured data in TypeScript, improving clarity and type safety.

Try it yourself

let user: [string, number] = ['TS', 10];

Test Your Knowledge

Q1
of 3

Tuple means?

A
Dynamic array
B
Fixed structure
C
Object
D
Function
Q2
of 3

Tuple length?

A
Fixed
B
Dynamic
C
Unlimited
D
Optional
Q3
of 3

Tuple types?

A
Same
B
Mixed
C
None
D
Any

Frequently Asked Questions

What is tuple?

An array with fixed types and order.

Tuple vs array?

Tuple has fixed structure, array is dynamic.

Can tuples be optional?

Yes, elements can be optional.

Previous

ts enums

Next

ts classes

Related Content

Need help?

Explore our comprehensive docs or start a chat with our tech experts.