Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the differences between readonly vs get in TypeScript properties?

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.

like image 243
krillgar Avatar asked Sep 05 '19 12:09

krillgar


People also ask

What does readonly do in TypeScript?

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.

What is diff between const and read only properties in TypeScript?

Summary: they are the same but const is for variables & readonly is for class properties.

How do you set a readonly property in TypeScript?

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 - .

What is the difference between const and readonly in angular?

const value initialized during declaration only, readonly can be declared without assign values. const value can not be reassigned, readonly can be reassigned.

How to make a property read-only in typescript?

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.

What is the difference between const and readonly in JavaScript?

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).

How to mark class properties as immutable in typescript?

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.

How do I assign a readonly property to a class?

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:


2 Answers

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.)

like image 145
T.J. Crowder Avatar answered Sep 22 '22 12:09

T.J. Crowder


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.

like image 41
Bergi Avatar answered Sep 19 '22 12:09

Bergi