typescript.api icon indicating copy to clipboard operation
typescript.api copied to clipboard

Inheritance issue, doesn't works every time

Open Vadorequest opened this issue 11 years ago • 2 comments

Hi, I have some trouble with inheritance in node.js server side.

Controller.ts

///<reference path='../../lib/def/defLoader.d.ts'/>

import express = require("express");

export module Controllers{
    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, '../../');

        constructor(){
            console.log('Build Master Controller');
        }

        private static  __before(req: express.Request, res: express.Response, next: Function){
            console.log('Call Parent Before');
            next();
        }


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

    }
}

UserController.ts

///<reference path='../../lib/def/defLoader.d.ts'/>

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

export module Controllers{
    export class UserController extends controller.Controllers.Controller{

        public prefix : string = "user";

        constructor() {
            console.log('Build User Controller');
        }

        public toast(req, res, next){
            console.log('J\'adooooorrrreeeee');

            if (req.cfgParam) {
                req.cfgParam.reply = "J'aime les Sushi";
            }

            next();
        }

        public test(req, res, next){
            console.log('test');

            if (req.cfgParam) {
                req.cfgParam.reply = "J'aime les Sushi";
            }

            next();
        }

        public default(req, res, next){
            console.log('default');
            req.cfgParam.reply = "J'aime les fruits";
            next();
        }
    }
}

I got the following error in the console when the server starts:

C:\wamp\www\Ayolan\game-server\app.js:109
troller = require("./app/controllers/UserController.ts").Controllers.UserContr
                                                                    ^
TypeError: Cannot read property 'UserController' of undefined
    at Object.<anonymous> (C:\wamp\www\Ayolan\game-server\app.js:109:80)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:902:3

There is the line in app.js:

var UserController = require("./app/controllers/UserController.ts").Controllers.UserController;

I have many troubles with inheritance with typescript.api. I tried many things but nothings works. (like use references with or instead imprort/require, with and without module, etc...)

Thank you.

Vadorequest avatar Dec 29 '13 16:12 Vadorequest

If I remove the extends and import/require in UserController, it works, without inheritance but works.

Vadorequest avatar Dec 29 '13 17:12 Vadorequest

The files are compiled in commonJs mode.

Vadorequest avatar Jan 01 '14 04:01 Vadorequest