DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Subtitles: The Good, the Bad, and the Resource-Heavy
  • Integrating Jenkins With Playwright TypeScript: A Complete Guide
  • Efficiently Migrating From Jest to Vitest in a Next.js Project
  • Implement a Geographic Distance Calculator Using TypeScript

Trending

  • Internal Developer Portals: Modern DevOps's Missing Piece
  • Beyond Linguistics: Real-Time Domain Event Mapping with WebSocket and Spring Boot
  • Streamlining Event Data in Event-Driven Ansible
  • Unlocking AI Coding Assistants Part 1: Real-World Use Cases
  1. DZone
  2. Coding
  3. JavaScript
  4. Typescript Tuples: How They Work

Typescript Tuples: How They Work

Let's look at how Typescript Tuples work.

By 
Johnny Simpson user avatar
Johnny Simpson
DZone Core CORE ·
Mar. 11, 22 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
7.0K Views

Join the DZone community and get the full member experience.

Join For Free

In Javascript, we are expecting a new primitive data type called a Tuple to appear soon. In Typescript, however, the concept of Tuple already exists.

A Tuple in Typescript is, much like in Javascript, an array, and it has a known length where every item has a known type.

How To Define a Tuple in Typescript

Defining a Tuple in Typescript is straightforward. All you need to do is define the type as an array of known types. For example, the following constant is a tuple:

TypeScript
 
const myTuple:[ string, number ] = [ "some", 15 ]

When we define first define a Tuple, it should be an array, it should have a known length, and each element should have a known type.

Defining an Array of Tuples in Typescript

We can also define an array of Tuples in Typescript. This is done by adding [] to the end of our Tuple type definition:

TypeScript
 
let myTuple:[ string, number ][] = [ [ "some", 15 ], [ "other", 20 ], [ "tuple", 50 ] ];

Mutating a Typescript Tuple

Unlike the tuple type in vanilla Javascript, Typescript Tuples by default are mutable - so they can be changed. As such, we can update a Tuple by simply referring to the element we want to update, and redefining it, assuming it isn't a constant:

TypeScript
 
let myTuple:[ string, number ] = [ "some", 15 ]
myTuple[1] = 20;

If we tried to change an element of our Tuple to a different type, however, we would get a type error. For example, if we tried to run myTuple[1] = "string", we would get the following error:

Plain Text
 
Type 'string' is not assignable to type 'number'.

Interestingly, if we try to extend the length of a Tuple by using myTuple.push(5), we will not get an error unless we are using push() on a constant, or if the type we use is not in the original type list.

This means that once a Tuple is defined, it no longer has to be of known length, as long as each element conforms to one of the original types we defined when first creating our Tuple. So myTuple.push(5) works in the case above - but myTuple.push(true) does not as true is boolean.

Creating Read-only Tuples in Typescript

If we want to create an immutable, read-only Tuple in Typescript, we can use the readonly keyword when defining our Tuple.

To do that, we would define our variable like this:

TypeScript
 
const myArray:readonly[number, string] = [10, 'test'];

If we try to mutate or change this Tuple, we will get an error. For example, if we use push() now, we will get the following error:

Plain Text
 
Property 'push' does not exist on type 'readonly [number, string]'.

Similarly, myArray[0] = 15 will return the following error:

Plain Text
 
Cannot assign to '0' because it is a read-only property.
Tuple TypeScript

Published at DZone with permission of Johnny Simpson, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Subtitles: The Good, the Bad, and the Resource-Heavy
  • Integrating Jenkins With Playwright TypeScript: A Complete Guide
  • Efficiently Migrating From Jest to Vitest in a Next.js Project
  • Implement a Geographic Distance Calculator Using TypeScript

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!