I am using AMD modules (compiler flag "--module amd") in my TypeScript project. While I can easily use getters/setters on my classes I would like to do the same on my modules but
export get abc() : string {
return "abc";
}
returns
error TS1008: Unexpected token; 'module, class, interface, enum, import or statement' expected.
and
export function get abc() : string {
return "abc";
}
returns
error TS1005: '(' expected.
What am I doing wrong?
In TypeScript, there are two supported methods getter and setter to access and set the class members.
The get method can be defined in a program to extract the value of any variable or to access the property of any object in TypeScript. The get keyword is used in the program along with the name of the method within which the code to be executed on the object. methodname() is written.
The TypeScript accessor provides us with a way to implement encapsulation. In simple terms, encapsulation hides the data members so that they can only be accessed by the member functions of the same class. The code snippet below provides an example of encapsulation .
You can only add getters and setters to a class at the moment.
The code transformation TypeScript uses on getters and setters adds the property to the prototype of the object, which makes more sense for classes than for modules.
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