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
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Beyond the Chatbot: Engineering a Real-World GitHub Auditor in TypeScript
  • Resilient API Consumption in Unreliable Enterprise Networks (TypeScript/React)
  • Supercharge AI Workflows on Azure: Remote MCP Tool Triggers + Your First TypeScript MCP Server
  • Tuples and Records (Part 5): Performance Challenges

Trending

  • The Missing `bandit` for AI Agents: How I Built a Static Analyzer for Prompt Injection
  • Securing the AI Host: Spring AI MCP Server Communication With API Keys
  • Building a Spring AI Assistant With MCP Servers: A Step-by-Step Tutorial
  • Beyond Manual Annotation: Engineering Self-Correcting Pseudo-Labeling Pipelines
  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
·
Mar. 11, 22 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
7.3K 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. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Beyond the Chatbot: Engineering a Real-World GitHub Auditor in TypeScript
  • Resilient API Consumption in Unreliable Enterprise Networks (TypeScript/React)
  • Supercharge AI Workflows on Azure: Remote MCP Tool Triggers + Your First TypeScript MCP Server
  • Tuples and Records (Part 5): Performance Challenges

Partner Resources

×

Comments

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

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook