javascriptnode.jsinheritancesails.jstypescript1.4

How to write a typescript corresponding to following javascript ?


var _ =require('lodash');
var Controller=function(){};
 Controller.prototype.index=function(req,res){
   res.ok();
 };
 Controller.extend=function(object){
      return _.extend({},Controller.prototype,object);
 };

I have tried a following typescript but it adds extend as Controller.prototype.extend instead of Controller.extend()

var _ = require('lodash');
class Controller
{
    index(){
        console.log("hi");
    }
    extends(object) {

    return _.extend({}, Controller.prototype, object);
    }
}

how can i change my typescript to obtain above javascript?


Solution

  • You are defining an instance method, which is part of the object's prototype, where you should be using a static method (part of the class).

    All you need to change is the declaration of extends(object) to:

    static extends(object) {
      ...
    }