Typescript: Accessors on class declarations

Created on 18 Sep 2016  路  4Comments  路  Source: microsoft/TypeScript

Dear sirs,

Would it be possible to add a get and set semantic on declaring classes? I was writing typescript declarations for the openUI5 project. They do not use the ECMA5 syntax property specification, but they use a getProperty setProperty syntax. I did not find any solution for this problem so far (maybe I am overlooking something).

It would be great to reflect these getters and setters as a property, as it would reduce the methods exposed via intellisense and creating a usage close to existing typescript classes.

Suggested Typescript Syntax

declare class ClassName {
  get PropertyName getMethod(): type;
  set PropertyName setMethod(value: type);
}

Example
_Javascript file to declare_

function Text() {}

Text.prototype.getText = function () {
    return this.Text;
}

Text.prototype.setText = function (value) {
    this.Text = value;
}

_Declaration file_

declare class {
  get Text getText(): string;
  set Text setMethod(value: string);
}

_Usage in Typescript file_

var helloworld = new Text();
text.Text = "Hello World";
alert(text.Text);

_Compiled output (Javascript)_

var helloworld = new Text();
text.setText("HelloWorld");
alert(text.getText());

Sources:
http://stackoverflow.com/questions/39534455/is-it-possible-to-have-getters-and-setters-in-typescript-declaration-file/39535551#39535551
https://github.com/apazureck/ui5.d.ts/tree/master/UI5Application1/UI5Application1/Scripts/typings/ui5

Question

Most helpful comment

@aluanhaddad That was actually what I was planning to do. As I already sweep their library and extract everything I need this would not be that problem. Hence, there are more important issues right now. And I have to clarify that with the main contributors, as it has to flow back in the project, as I do not want a parallel version.
Thanks for your help!

All 4 comments

You can't do this purely at the declaration level. Declaration files do not emit any code. If you have access to the source you can of course modify it to use properties, which, I would add, would be of as much benefit to JavaScript consumers as TypeScript consumers. In short you need to wrap the library.

Ok, thank you for that information. Would be a nice feature, though :)

@apazureck you could probably write a parser pretty easily to transform the entire library by augmenting it with properties that delegate to the getX and setX methods.

Then you could leverage TypeScript to verify that you didn't introduce any name collisions. Then you could create a .d.ts file annotating the getX and setX methods with private to hide them from the public API at design time.

Probably not worth the time but it could be fun ;)

@aluanhaddad That was actually what I was planning to do. As I already sweep their library and extract everything I need this would not be that problem. Hence, there are more important issues right now. And I have to clarify that with the main contributors, as it has to flow back in the project, as I do not want a parallel version.
Thanks for your help!

Was this page helpful?
0 / 5 - 0 ratings