DaveC
DaveC

Reputation: 552

In Typescript how do I set a new class as the prototype?

This is the output I want to achieve:

MyTypeScriptClass.prototype = new ko.templateEngine();

And this is my TypeScript:

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

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

Upvotes: 1

Views: 125

Answers (1)

Remo H. Jansen
Remo H. Jansen

Reputation: 25029

You should be able to use something like the following:

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 */;
    }
}

The output ES5 code is quite different because it uses the __extends helper:

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;

But the behaviour should be the same.

Upvotes: 1

Related Questions