Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Tuple Types | Core TypeScript Types
TypeScript Foundations

bookTuple Types

Tuples in TypeScript let you create arrays with a fixed number of elements, where each element can have a different, specific type. This is different from regular arrays, which can hold any number of elements of a single type. The syntax for a tuple uses square brackets, listing the types of each element in order. For example, a tuple that holds a string and a number would be written as [string, number]. Tuples are especially useful when you need to represent a structured group of values, such as a pair of coordinates or a result with a value and a status.

index.ts

index.ts

copy

While both arrays and tuples use similar syntax, they serve different purposes. Arrays are best when you need a list of items of the same type, such as a list of numbers or strings. Tuples, on the other hand, are best for representing a fixed collection of values with known types and order. For example, use a tuple when you want to return multiple values from a function, or when you need to store pairs or triples of different types. If the number of elements or their types might change, use an array instead.

question mark

Which statement best describes a tuple in TypeScript?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 7

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

Suggested prompts:

Can you give more examples of tuples in TypeScript?

What are some common use cases for tuples versus arrays?

How do you access or modify elements in a tuple?

Awesome!

Completion rate improved to 5.56

bookTuple Types

Swipe um das Menü anzuzeigen

Tuples in TypeScript let you create arrays with a fixed number of elements, where each element can have a different, specific type. This is different from regular arrays, which can hold any number of elements of a single type. The syntax for a tuple uses square brackets, listing the types of each element in order. For example, a tuple that holds a string and a number would be written as [string, number]. Tuples are especially useful when you need to represent a structured group of values, such as a pair of coordinates or a result with a value and a status.

index.ts

index.ts

copy

While both arrays and tuples use similar syntax, they serve different purposes. Arrays are best when you need a list of items of the same type, such as a list of numbers or strings. Tuples, on the other hand, are best for representing a fixed collection of values with known types and order. For example, use a tuple when you want to return multiple values from a function, or when you need to store pairs or triples of different types. If the number of elements or their types might change, use an array instead.

question mark

Which statement best describes a tuple in TypeScript?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 7
some-alt