How to add readonly attribute to declaration file?

I am creating a declaration file for a library that does not yet exist, but there are some classes that have readonly attributes. How can I put this in an ad?

+5
source share
2 answers

Strange, you cannot specify getters and setters on interfaces or on ambient ads.

Closest you can get:

interface Test {
    Prop: number;
}

class Example implements Test {
    private prop: number;

    get Prop() :number {
        return this.prop;
    }
}

Unfortunately, this does not interfere with the implementation of allowing the setter in the context of the announcement of the environment, it does not prevent the caller from setting the value.

+5
source

With TypeScript 2.0, you can now declare readonly properties, for example

interface Point {
    readonly x: number;
    readonly y: number;
}

var p1: Point = { x: 10, y: 20 };
p1.x = 5;                            // Error, p1.x is read-only

var p2 = { x: 1, y: 1 };
var p3: Point = p2;                  // Ok, read-only alias for p2
p3.x = 5;                            // Error, p3.x is read-only
p2.x = 5;                            // Ok, but also changes p3.x because of aliasing

see here for more details.

+2
source

All Articles