A
A
Alex2020-02-15 18:24:22
typescript
Alex, 2020-02-15 18:24:22

How to get the readonly property of a class to compile correctly?

There is a simple class

By default TypeScript uses readonlyonly during the type checking phase. Properties get into JS itself without modifications:

class MyClass {
  public readonly prop = 1
}


class MyClass {
    constructor() {
        Object.defineProperty(this, "prop", {
            enumerable: true,
            configurable: true, // <--
            writable: true, // <--
            value: 1
        });
    }
}


Is there a way to force TypeScript to compile readonly properties so they can't be changed at runtime?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Aetae, 2020-02-15
@Kozack

And why do you need it? What ironclad reason do you have for that?
And so, you can always add your own written loader to the assembly chain that will perform any transformations you need.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question