How to make object properties immutable in TypeScript ? Last Updated : 16 Feb, 2022 Comments Improve Suggest changes Like Article Like Report In this article, we will try to understand how we could make object properties immutable in TypeScript. JavaScript is a highly dynamic and flexible language, hence making object properties in JavaScript immutable is a little bit typical (although we may implement it somehow using a const data type that doesn't work always). So here in TypeScript we will try to implement the immutability of attributes using a keyword called readonly. This keyword could make the object property unchangeable, which is non-alterable but could only be accessed rather than could be modified. Syntax: Following is the syntax of using the readonly keyword in TypeScript: class class_name { readonly property_name : string ... }Now that you have some basic idea of implementing this keyword let us see some of the examples in which we could use this readonly keyword. Example 1: In this example we will be creating a class consisting of several properties and then we will try to read the property values and will also try to update those values and will the result in the console. JavaScript <script> class UserDetails { readonly name: string readonly city: string readonly phoneNo: number constructor(name: string, city: string, phoneNo: number){ this.name = name this.city = city this.phoneNo = phoneNo } } let user1 = new UserDetails("ABCD", "XYZ", 1234567890); let user2 = new UserDetails("APPLE", "PQR", 1269785100); // Printing values console.log(user1.city); console.log(user2.city); console.log(user1.phoneNo); // user1.city = "ABCDEF"; Not possible will result in error </script> Output: XYZ PQR 1234567890Example 2: In this example we will be creating an interface and then further using that interface we will create an object and assign some values to it some being readonly only and then will try to update some values and visualize the result in console. JavaScript <script> interface UserDetails { readonly firstName: string; lastName: string; } let user: UserDetails = { firstName: "Aman", lastName: "XYZ", }; // Printing user object values before it is changed console.log(user); user.lastName = "Singla"; // Printing user object values after it is changed console.log(user); // user.firstName = "APPLE"; Cannot assign // value to firstName because it is a // read-only property. </script> Output: { firstName: 'Aman', lastName: 'XYZ' } { firstName: 'Aman', lastName: 'Singla' } Reference: https://www.typescriptlang.org/play#example/immutability Comment More infoAdvertise with us Next Article How to make object properties immutable in TypeScript ? amansingla Follow Improve Article Tags : JavaScript Web Technologies Geeks Premier League TypeScript Geeks-Premier-League-2022 JavaScript-Questions +1 More Similar Reads How to Iterate Over Object Properties in TypeScript In TypeScript, Objects are the fundamental data structures that use key-value pair structures to store the data efficiently. To iterate over them is a common task for manipulating or accessing the stored data. TypeScript is a superset of JavaScript and provides several ways to iterate over object pr 3 min read How to Make a Single Property Optional in TypeScript ? TypeScript is a popular programming language that is a strict syntactical superset of JavaScript, adding optional static typing and class-based object-oriented programming to the language. One useful feature of TypeScript is the ability to specify optional properties in interfaces and classes, allow 5 min read How do I dynamically assign properties to an object in TypeScript? In TypeScript, we can not assign the properties dynamically to an object directly by using the dot operator and the square brackets syntax which we generally use to assign dynamic properties in Vanilla JavaScript. JavaScript is a dynamically typed language and the type of a variable is determined at 3 min read TypeScript Object Type Property Modifiers TypeScript Type Property Modifiers are used to specify each property in an object such as: the type, whether the property is optional, and whether the property can be written to. TypeScript Object Type Property Modifiers:readonly Properties: An object type with readonly properties specifies that the 3 min read How to Specify Optional Properties in TypeScript? TypeScript is a powerful programming language that extends JavaScript by adding optional static typing and class-based object-oriented programming. One of the key features of TypeScript is the ability to specify optional properties in interfaces and classes, providing flexibility to our object types 3 min read How to Cast Object to Interface in TypeScript ? In TypeScript, sometimes you need to cast an object into an interface to perform some tasks. There are many ways available in TypeScript that can be used to cast an object into an interface as listed below: Table of Content Using the angle bracket syntaxUsing the as keywordUsing the spread operatorU 3 min read How to Deep Merge Two Objects in TypeScript ? Merging two objects in TypeScript is a common task, but when dealing with complex nested structures, a deep merge becomes necessary. A deep merge combines the properties of two or more objects, including nested objects, creating a new object with merged values. In this article, we will explore vario 5 min read How to Sort or Reduce an Object by Key in TypeScript ? Sorting an object by key generally refers to arranging its properties in a specific order, while reducing involves selecting a subset of properties based on provided keys. Different approaches allow developers to perform these operations with flexibility.Below are the approaches used to sort or redu 3 min read How to Initialize a TypeScript Object with a JSON-Object ? To initialize a TypeScript Object with a JSON-Object, we have multiple approaches. In this article, we are going to learn how to initialize a TypeScript Object with a JSON-Object. Below are the approaches used to initialize a TypeScript Object with a JSON-Object: Table of Content Object.assign Type 3 min read How to Remove a Key from TypeScript Object ? In TypeScript, objects are mutable, which means you can modify them by adding, updating, or removing properties. Removing a key from an object involves either creating a new object without the specified key or using certain built-in methods to achieve the desired result.Below are the approaches used 4 min read Like