Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Unable to use TypeScript server side with node.js (Inheritance only)

Unable to use TypeScript server side with node.js (Inheritance only)

Problem

I don't understand why inheritance in TS is so tricky. It doesn't works on my 'Controllers'. I tried to compile with both AMD and commonJS, same result.

UserController

///
///

import express = require("express");
import controller = require("./Controller");

export class UserController extends controller.Controller{

    public constructor(req: express.Request, res: express.Response){
        super();
    }

    public  __before(){
        console.log('before');
    }

    public  __repartitor(){
        console.log('repartitor');
    }

    public  test(){
        console.log('test');
    }

    public  default(){
        console.log('default');
    }

}

Controller (parent)

///
import express = require("express");

export class Controller{

    public static beforeSharedPath: string = path.join(__dirname, "../../public");
    public static publicPath: string = path.join(__dirname, '../../');
    public static sharedPath: string = Controller.beforeSharedPath+"/shared";
    public static webServerPath: string = path.join(__dirname, '../../');

    public req: express.Request;
    public res: express.Response;

    public constructor(req: express.Request, res: express.Response){
        this.req = req;
        this.res = res;
    }

    public static __before(req: express.Request, res: express.Response, next: Function){
        next();
    }

    public static __after(req: express.Request, res: express.Response, next: Function){
        next();
    }

}

In another script:

 var obj = require('./../app/controllers/' + controllerName+".js")

The previous code call:

module.exports = require('./ts/UserController.ts').UserController;

But I got a obj undefined. If I remove the extends and call() I got the object. I just don't understand, I tried to fix it 3 hours without get any good result.

There is some tutorial to use TS on nodejs server side? ... I use TS somewhere else and it works for both server + client with the same code, but it's the same, when I extends I got troubles.

Problem courtesy of: Vadorequest

Solution

Your UserController and Controller code is valid and it should work.

Error seems to be in var obj = require('./../app/controllers/' + controllerName+".js") You should not need the js extension.

Solution courtesy of: basarat

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Unable to use TypeScript server side with node.js (Inheritance only)

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×