masimplo
masimplo

Reputation: 3774

Is it possible to chain accessors or sth similar in typescript?

I have something like this in javascript:

MyObject.prototype.route = function(route) {
  this.route = route;
  return this;
};

The required usage would be:

myInstance.route('hello').otherFunctionOfMyObject();

and afterwards as

var text = myInstance.route;

How would I go about writing something similar in typescript who is more strict about this.route being either a function or a property?

I tried using a typescript accessor to no avail.

Upvotes: 0

Views: 154

Answers (2)

basarat
basarat

Reputation: 275947

FYI your javascript is wrong. As soon as you call this function

MyObject.prototype.route = function(route) { // RouteA function
  this.route = route; // RouteB
  return this;
};

Then you can no longer access the RouteA function using instance.route because instance.route points to the RouteB passed into RouteA and JavaScript does not look at instance.__proto__.route (which is RouteA).

Upvotes: 0

basarat
basarat

Reputation: 275947

You can use any and just port over you JavaScript to TypeScript.

If you must use classes then you can't do that because a member of a class can only be either a function or a getter not both

Upvotes: 1

Related Questions