Is there a functional difference between declaring a property in TypeScript as readonly
vs creating it through a get()
? Both behave the same, but it'd be good to know if there is a reason to use one over the other besides preference.
TypeScript - ReadOnly TypeScript includes the readonly keyword that makes a property as read-only in the class, type or interface. Prefix readonly is used to make a property as read-only. Read-only members can be accessed outside the class, but their value cannot be changed.
Summary: they are the same but const is for variables & readonly is for class properties.
You can use mapping modifiers to change a readonly property to mutable in TypeScript, e.g. -readonly [Key in keyof Type]: Type[Key] . You can remove the readonly modifier by prefixing the readonly keyword with a minus - .
const value initialized during declaration only, readonly can be declared without assign values. const value can not be reassigned, readonly can be reassigned.
TypeScript includes the readonly keyword that makes a property as read-only in the class, type or interface. Prefix readonly is used to make a property as read-only.
Summary: they are the same but const is for variables & readonly is for class properties. One of the key difference between const and readonly is in how it works with the array. (appart form already ans diff) You have to use while working with Array, where T is generic type (google it for more).
Summary: in this tutorial, you will learn how to use the TypeScript readonly access modifier to mark class properties as immutable property. TypeScript provides the readonly modifier that allows you to mark the properties of a class immutable. The assignment to a readonly property can only occur in one of two places: In the property declaration.
The assignment to a readonly property can only occur in one of two places: In the property declaration. In the constructor of the same class. To mark a property as immutable, you use the readonly keyword. The following shows how to declare a readonly property in the Person class:
It makes a difference to the generated JavaScript: The getter will be an accessor property (e.g., function), the readonly
property will be a data property. This:
class Example {
get foo(): string {
return "foo";
}
readonly bar: string = "bar";
}
converts to this if you target ES2015+:
"use strict";
class Example {
constructor() {
this.bar = "bar";
}
get foo() {
return "foo";
}
}
Live on the playground
or to this if you target ES5+:
"use strict";
var Example = /** @class */ (function () {
function Example() {
this.bar = "bar";
}
Object.defineProperty(Example.prototype, "foo", {
get: function () {
return "foo";
},
enumerable: true,
configurable: true
});
return Example;
}());
Live on the playground
Notice that although TypeScript considers bar
read-only, nothing enforces that at runtime. foo
, on the other hand, is read-only even at runtime. (Although if you really wanted to, you could use Object.defineProperty
to defeat it by reconfiguring the property.)
A getter is evaluated again on every property access, and creates a new value. This is an important functional difference, as that behaviour is usually not what you want:
class Example {
get foo(): object {
return {};
}
readonly bar: object = {};
}
const example: Example = new Example;
console.log(example.foo === example.foo); // false
console.log(example.bar === example.bar); // true
For the implementation details, see @T.J.Crowder's excellent answer which I won't repeat here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With