const vs readonly
Written byPhuoc Nguyen
Created
20 Aug, 2020
Category
TypeScript
#Differences
-
`const`
is used for variablesjsconst message = 'Hello';// Does not workmessage = 'World';While`readonly`
is used for properties. The properties can be declared as a member of classjsclass Triangle {public readonly numberOfVertices = 3;}const triangle = new Triangle();// Does not worktriangle.numberOfVertices = 4;or`type`
,`interface`
:jsinterface Person {firstName: string;lastName: string;readonly fullName: string;} -
`const`
declarations have to be initialized, and you can't reassign their values. The`readonly`
properties can be reassigned in the constructor function.jsclass Square {readonly numberOfVertices: number;constructor() {this.numberOfVertices = 4;}}The`readonly`
properties could be changed if we don't pass their class or interface directly but passing an alias.Let's take a look at the`Person`
interface above, and assume that we have the following function to update the person information:jsconst updatePerson = (person: { firstName: string, lastName: string, fullName: string }) => {person.fullName = `\${firstName}, \${lastName}`;};We can update the`fullName`
property because it's an property of`person`
parameter:jslet person: Person = {firstName: 'Foo',lastName: 'Bar',fullName: 'Foo Bar',};updatePerson(person);person.fullName; // `Foo, Bar`Of course, the compiler will throw an error if we pass the original type`Person`
:jsconst updatePerson = (person: Person) => {// Error: Cannot assign to 'fullName' because it is a read only propertyperson.fullName = `\${person.firstName}, \${person.lastName}`;};
#Good to know
-
In a given class, if a property has only getter method and doesn't come with setter method, it will be treated as read only.jsclass Square {side: number = 0;get area() {return this.side * this.side;}}const s = new Square();Setting
`s.area = 100`
will throw an error because`area`
is a ready only property. -
In the React library, we don't change the props and state of a component directly. Because the props are immutable and the state could be updated via
`setState()`
method.React type definitions wrap the props and state in read only type.js// P, S represents the props and state respectivelyclass Component<P, S> {constructor(props: Readonly<P>);readonly props: Readonly<P> & Readonly<{ children?: ReactNode }>;state: Readonly<S>;}
Questions? 🙋
Do you have any questions about front-end development? If so, feel free to create a new issue on GitHub using the button below. I'm happy to help with any topic you'd like to learn more about, even beyond what's covered in this post.
While I have a long list of upcoming topics, I'm always eager to prioritize your questions and ideas for future content. Let's learn and grow together! Sharing knowledge is the best way to elevate ourselves 🥷.
Recent posts ⚡
Newsletter 🔔
If you're into front-end technologies and you want to see more of the content I'm creating, then you might want to consider subscribing to my newsletter.
By subscribing, you'll be the first to know about new articles, products, and exclusive promotions.
Don't worry, I won't spam you. And if you ever change your mind, you can unsubscribe at any time.
Phước Nguyễn