I'm creating a declarationsfile for a library that doesnt exist yet, but there are some classes in there that have readonly attributes. How can i put that in the declaration?
Use setAttribute() Method to add the readonly attribute to the form input field using JavaScript. setAttribute() Method: This method adds the defined attribute to an element, and gives it the defined value. If the specified attribute already present, then the value is being set or changed.
The Boolean readonly attribute, when present, makes the element not mutable, meaning the user can not edit the control. If the readonly attribute is specified on an input element, because the user can not edit the input, the element does not participate in constraint validation.
A read-only field cannot be modified. However, a user can tab to it, highlight it, and copy the text from it. Tip: To prevent the user from interacting with the field, use the disabled property instead.
Projects In JavaScript & JQuery To make a textarea and input type read only, use the attr() method .
Strangely, you can't specify getters and setters on interfaces or on ambient declarations.
The closest you can get is:
interface Test {
Prop: number;
}
class Example implements Test {
private prop: number;
get Prop() :number {
return this.prop;
}
}
Unfortunately, this doesn't prevent the implementation from allowing a setter and in the context of an ambient declaration it wouldn't prevent the caller from attempting to set the value.
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.
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