Cannot instantiate a type script class in another file. Error: ts2351

Question

Problem: I cannot seem to instantiate a new instance of a type script class which I am importing from another file.

Explanation: I am writting a node application in type script which uses the express module. These classes are in external files as to encapsulate the routing of express.

The file 'masterRoute.ts' is to forward any incoming http requests to their intended route.ts file.

The file 'indexRoute.ts' is the intended route file for the url: 'whatever.com/'.

Aim: For learning purposes, I am trying to make an instance of the external IndexRoute class in the MasterRoute class.

Example:

indexRoute.ts

import express = require('express');
export class IndexRoute{
    private _ejsFilePath: string = 'views/index.ejs';
    private request: express.Request;
    private response: express.Response;
    constructor(request: express.Request, response: express.Response){
        this.request = request;
        this.response = response;
    }
    public renderFile(): string{
        try{
            this.response.render(this._ejsFilePath);
            return null;
        }
        catch(error){
            return error;
        }
    }
}

masterRoute.ts

import express = require('express');
import * as IndexRoute from './indexRoute';
export class MasterRoute{
    private Router: express.Router;
    constructor(){
        this.Router = new express.Router();
    }
    public onHttpRequest(request: express.Request, response: express.Response){
        // I have tried to use all of the lines below one by one. 
        // None of them work.
        // let test: IndexRoute = new IndexRoute(request, response);
        // let test: = new IndexRoute(request, response);
        // let test = IndexRoute(request, response); 
        this.Router.get('/');
    }
}

Show source
| class   | node.js   | express   | typescript   | instantiation   2017-01-07 07:01 0 Answers

Answers to Cannot instantiate a type script class in another file. Error: ts2351 ( 0 )

Leave a reply to - Cannot instantiate a type script class in another file. Error: ts2351

◀ Go back