3

Node7.4.0 / ES6 / Typescript 2.1.5 / WebStorm 2016.3

On the line : export default heroRoutes.router;

I get: TS2503 Cannot find namespace 'heroRoutes' after creating it and init() what could be wrong with it ?

thanks for feedback

HeroRouter.ts

import {Router, Request, Response, NextFunction} from 'express';
const Heroes = require('../data');

export class HeroRouter {
    router: Router;

    /**
     * Initialize the HeroRouter
     */
    constructor() {
        this.router = Router();
        this.init();
    }

    /**
     * GET all Heroes.
     */
    public  getAll(req: Request, res: Response, next: NextFunction) {
        res.send(Heroes);
    }

    /**
     * GET one hero by id
     */
    public getOne(req: Request, res: Response, next: NextFunction) {
        let query = parseInt(req.params.id);
        let hero = Heroes.find(hero => hero.id === query);
        if (hero) {
            res.status(200)
                .send({
                    message: 'Success',
                    status: res.status,
                    hero
                });
        }
        else {
            res.status(404)
                .send({
                    message: 'No hero found with the given id.',
                    status: res.status
                });
        }
    }

    /**
     * Take each handler, and attach to one of the Express.Router's
     * endpoints.
     */
    init() {
        this.router.get('/', this.getAll);
        this.router.get('/:id', this.getOne);
    }

}

// Create the HeroRouter, and export its configured Express.Router
let heroRoutes = new HeroRouter();
heroRoutes.init();

export default heroRoutes.router;

1 Answer 1

5
const heroRouter = new HeroRouter();
const router = heroRouter.router;
export default router;

The reason for this is that you cannot export a qualified name. The exports of a module are bound to a special object known as the module namespace object. One reason is that if qualified exports were legal, the semantics would be surprising as updating the value of the instance member router of the variable heroRouter would not update the value of the exported binding (here named default).

Sign up to request clarification or add additional context in comments.

5 Comments

thanks Aluan , that's the point ! I should also not forget to insert the heroRouter.init() between the two const declarations... what's the point here ? is it in the export default parameter ... any link to some doc ?...
Indeed there's a bit of context missing, I updated my answer
As for the init method, I would get rid of it and put all the logic in the constructor. It is a bad practice to allow objects to be created in an invalid state. It leads to bugs, like the one in my example in the context of your question.
thanks a lot for your helpful comments... can it be rewritten as : const heroRouter = new HeroRouter().router; export default heroRouter;
Yes if you drop the init method or make it chain the instance and call it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.