2017-06-06 38 views
1

這是我想達到的輸出:In Typescript如何設置一個新的類作爲原型?

MyTypeScriptClass.prototype = new ko.templateEngine();

這是我的打字稿:

module Knockout { 
    export class MyTypeScriptClass implements KnockoutNativeTemplateEngine { 
     allowTemplateRewriting: boolean = false; 

     renderTemplateSource(templateSource, bindingContext, options) { 
      // does some custom work 
     } 
    } 
} 

回答

1

您應該能夠使用類似以下內容:

import * as ko from "knockout"; 

export class MyTypeScriptClass extends ko.templateEngine { 
    allowTemplateRewriting: boolean = false; 

    public renderTemplateSource(
     templateSource: Object, 
     bindingContext: KnockoutBindingContext, 
     options: Object) { 

      return /* does some custom work */; 
    } 
} 

輸出ES5代碼非常不同,因爲它使用__extends幫手:

var __extends = (this && this.__extends) || function (d, b) { 
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; 
    function __() { this.constructor = d; } 
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); 
}; 

var ko = require("knockout"); 

var MyTypeScriptClass = (function (_super) { 
    __extends(MyTypeScriptClass, _super); 
    function MyTypeScriptClass() { 
     var _this = _super.apply(this, arguments) || this; 
     _this.allowTemplateRewriting = false; 
     return _this; 
    } 
    MyTypeScriptClass.prototype.renderTemplateSource = function (templateSource, bindingContext, options) { 
     return []; 
    }; 
    return MyTypeScriptClass; 
}(ko.templateEngine)); 

exports.MyTypeScriptClass = MyTypeScriptClass; 

但行爲應該是一樣的。

相關問題