编译后的Typescript错误无法设置undefined的属性

时间:2017-02-02 22:19:13

标签: node.js typescript

我正在尝试使用NodeJ构建应用。我熟悉原始Node.JS但是Typescript是新的。

我想要完成的是让它运行但我在再次编译成Javascript后继续遇到此错误。 Cannot set property "title" of undefined

所以我有两个类,一个看起来像这样的基类:

import { Request, Response, Router } from 'express';

export class BaseRoute {

    public basePath: string; // this gets set in child constructors
    public title: string;
    private scripts: string[];

    constructor() {
        this.title = "";
        this.scripts = [];
    }

}

继承的人看起来像这样:

import { Request, Response, NextFunction, Router } from 'express';
import { BaseRoute } from './BaseRoute';

export class IndexRoute extends BaseRoute {

    constructor() {
        super();
        this.basePath = "/";
    }

    index(req: Request, res: Response, next: NextFunction) {
        this.title = "Home | Page";
        let options: Object = {
            "message": "This is a test message"
        };

        this.render(req, res, "index", options);
    }

}

它很好地转化为Js,但是当我尝试去路线/时,我得到了错误。堆栈告诉我它被IndexRoute.index

抛出

转换后的版本看起来像:

BaseRoute:

"use strict";
var express_1 = require("express");
var BaseRoute = (function () {
    function BaseRoute() {
        this.title = "";
        this.scripts = [];
    }
    return BaseRoute;
}());
exports.BaseRoute = BaseRoute;

IndexRoute:

"use strict";
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 express_1 = require("express");
var BaseRoute_1 = require("./BaseRoute");
var IndexRoute = (function (_super) {
    __extends(IndexRoute, _super);
    function IndexRoute() {
        var _this = _super.call(this) || this;
        _this.basePath = "/";
        return _this;
    }
    IndexRoute.prototype.index = function (req, res, next) {
        this.title = "Home | Page";
        var options = {
            "message": "This is a test message"
        };
        this.render(req, res, "index", options);
    };
    return IndexRoute;
}(BaseRoute_1.BaseRoute));
exports.IndexRoute = IndexRoute;

0 个答案:

没有答案