bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/TypeScript/TypeScript Core
TypeScript•TypeScript Core

TypeScript Tuples

Typed Arrays

A tuple is a typed array with a pre-defined length and types for each index.

Tuples are great because they allow each element in the array to be a known type of value.

To define a tuple, specify the type of each element in the array:

Example

// define our tuple
let ourTuple: [number, boolean, string];
// initialize correctly ourTuple = [5, false, 'Coding God was here'];

As you can see we have a number, boolean and a string.

But what happens if we try to set them in the wrong order:

Example

// define our tuple
let ourTuple: [number, boolean, string];
// initialized incorrectly which throws an error ourTuple = [false, 'Coding God was mistaken', 5];

Even though we have a boolean , string , and number the order matters in our tuple and will throw an error.

Readonly Tuple

A good practice is to make your tuple readonly .

Tuples only have strongly defined types for the initial values:

Example

// define our tuple
let ourTuple: [number, boolean, string];
// initialize correctly ourTuple = [5, false, 'Coding God was here']; // We have no type safety in our tuple for indexes 3+ ourTuple.push('Something new and wrong'); console.log(ourTuple);

You can see the new value.

Tuples only have strongly defined types for the initial values:

Example

// define our readonly tuple
const ourReadonlyTuple: readonly [number, boolean, string] = [5, true, 'The Real Coding God'];
// throws error as it is readonly. ourReadonlyTuple.push('Coding God took a day off');

To learn more about access modifiers like readonly go to our section on them here: TypeScript Classes .

If you have ever used React before you have worked with tuples more than likely.

useState returns a tuple of the value and a setter function.

const [firstName, setFirstName] = useState('Dylan') is a common example.

Because of the structure we know our first value in our list will be a certain value type in this case a string and the second value a function .

Named Tuples

Named tuples allow us to provide context for our values at each index.

Example

const graph: [x: number, y: number] = [55.2, 41.3];

Named tuples provide more context for what our index values represent.

Destructuring Tuples

Since tuples are arrays we can also destructure them.

Example

const graph: [number, number] = [55.2, 41.3];
const [x, y] = graph;

To review destructuring check it out here .

Previous

TypeScript Arrays

Next

TypeScript Object Types

This chapter

Overview
40

Lessons

241m

Read time

1. TypeScript Tutorial2. TypeScript Introduction3. TypeScript Getting Started4. TypeScript Simple Types5. TypeScript Explicit Types and Inference6. TypeScript Special Types7. TypeScript Arrays8. TypeScript Tuples9. TypeScript Object Types10. TypeScript Enums11. TypeScript Type Aliases and Interfaces12. TypeScript Union Types13. TypeScript Functions14. TypeScript Casting15. TypeScript Classes16. TypeScript Basic Generics17. TypeScript Utility Types18. TypeScript Keyof19. TypeScript Null & Undefined20. TypeScript Definitely Typed21. TypeScript 5.x Updates22. TypeScript Configuration23. TypeScript with Node.js24. TypeScript with React25. TypeScript Tooling26. TypeScript Advanced Types27. TypeScript Type Guards28. TypeScript Conditional Types29. TypeScript Mapped Types30. TypeScript Type Inference31. TypeScript Literal Types32. TypeScript Namespaces33. TypeScript Index Signatures34. TypeScript Declaration Merging35. TypeScript Async Programming36. TypeScript Decorators37. TypeScript in JavaScript Projects (JSDoc)38. TypeScript Migration Guide39. TypeScript Error Handling40. TypeScript Best Practices

On this page

Typed ArraysReadonly TupleNamed TuplesDestructuring Tuples