Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getter/setter on a module in TypeScript

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?

like image 661
daniel.sedlacek Avatar asked Mar 03 '15 14:03

daniel.sedlacek


People also ask

Can we use getter and setter in TypeScript?

In TypeScript, there are two supported methods getter and setter to access and set the class members.

How do you use GET method in TypeScript?

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.

What are accessors in TypeScript?

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 .


1 Answers

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.

like image 194
Fenton Avatar answered Sep 18 '22 14:09

Fenton