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
  • TypeScript in Cloud Applications: Why It’s a Powerful Choice

Trending

  • Mastering Fluent Bit: Beginners' Guide for Contributing to our CNCF Project Docs
  • 8 Major Challenges Faced by Android Application Developers
  • Reproducible Development Environments, One Command Away: Introducing CodingBooth
  • Logging What AI Agents Do in Salesforce: A Simple One-Object Audit Framework
  1. DZone
  2. Coding
  3. JavaScript
  4. How the TypeScript Readonly Type Works

How the TypeScript Readonly Type Works

Let's take a look at how the Read-only type works in TypeScript.

By 
Johnny Simpson user avatar
Johnny Simpson
·
Apr. 25, 22 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
3.9K Views

Join the DZone community and get the full member experience.

Join For Free

TypeScript has a number of utility types, which are types specifically created by TypeScript to solve a problem. In this article, let's look at the Readonly type.

TypeScript Readonly Type

As the name suggests, the Readonly type in TypeScript suggests that a particular type is read-only.

Let's look at an example. Below, we don't want anyone to update any part of myObject. We can make it a read-only object like so:

TypeScript
 
type User = {
    firstName: string,
    lastName: string
}

let firstUser:Readonly<User> = {
    firstName: "John",
    lastName: "Doe"
}

If you try to change a property of firstUser, you'll get the following error:

Plain Text
 
Cannot assign to 'firstName' because it is a read-only property.

Readonly Variables Don’t Work in Typescript

When we define the type User above, we are creating a custom interface - i.e. something to which objects have to conform to. Readonly only works with interfaces or custom types like the one we've used. As such, we can still edit Readonly variables:

TypeScript
 
let myVariable:Readonly<string> = "Hello World";
myVariable = "Goodbye World";
console.log(myVariable); // console logs "Goodbye World"

The above code is valid and will work in TypeScript. If you need read-only variables, you can simply use const instead, i.e:

TypeScript
 
const myVariable:string = "Hello World";
// Throws error: Cannot assign to 'myVariable' because it is a constant.
myVariable = "Goodbye World";
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
  • TypeScript in Cloud Applications: Why It’s a Powerful Choice

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