4

I've been pouring through StackOverflow posts and multiple blog posts, but can't seem to find the answer.

I'm using VS Code, nodejs, typescript and gulp. When I place all of my module Controllers code into one file like this:

 <controllers.ts>
 module Controllers {
    export class Api {
         get(controllerName:string):IController {
         }
    }   
 }

 module Controllers {
    export interface IController {
        performAction(actionName:string):any;
    }
 }

 module Controllers {
    export class ControllerBase implements IController {
        performAction(actionName:string):any {
    }
  }
}

module Controllers {
    export class UserController extends ControllerBase {
    }
}

export = Controllers;

Gulp is able to compile it and nodejs runs it correctly.

When I break it into multiple files though, I can't seem to get it to compile:

<controllers.ts>
module Controllers {
    export class Api {
         get(controllerName:string):IController {
         }
    }   
}

module Controllers {
    export interface IController {
        performAction(actionName:string):any;
    }
}

module Controllers {
    export class ControllerBase implements IController {
        performAction(actionName:string):any {
        }
    }
}

export = Controllers;

<UserController.ts>
module Controllers {
    export class UserController extends ControllerBase {
    }
}

I've tried adding

/// <reference path="./controllers.ts" /> 

to the top of my UserController.ts file. When I do that, it still can't find ControllerBase.

I tried doing

export class Controllers.UserController extends Controllers.ControllerBase

and it doesn't like the Controllers before UserController, but if I remove that Controllers and make it look like this:

export class UserController extends Controllers.ControllerBase

then obviously it places UserController in a different module.

Also, importing it like this:

import Controllers = require("./controllers");

doesn't seem to help either. I'm not sure what else to try. Any help would be greatly appreciated. It doesn't seem like this shouldn't be achievable.

2 Answers 2

1

When looking at doing something like this, there seem to be two issues.

  1. Intellisense in your coding tool of choice and
  2. Compilation.

For my scenario, intellisense isn't a concern, because I shouldn't need intellisense on the functions in the base class for the classes that extend the base class. Compilation was my big struggle. Here is the solution I came up with. I don't love it, but it does seem to work.

var gulp = require('gulp');
var concat = require('gulp-concat');
var insert = require('gulp-insert');
var ts = require('gulp-typescript');
var del = require('del');

var tsOptions = {
    noImplicitAny: true,
    target: 'ES5',
    module: 'commonjs'
};

gulp.task('clean', function(done) {
    del(['./dest'], done);
})

gulp.task('compile-controllers', function() {
    return gulp.src(['./src/controllers.ts', './src/*.ts'])
               .pipe(concat('controllers.ts'))
               .pipe(insert.append('export = Controllers;'))
               .pipe(ts(tsOptions))
               .js
               .pipe(gulp.dest('dest/server'));
});
Sign up to request clarification or add additional context in comments.

Comments

0

Typescript Internal Module Across Multiple Files In Nodejs

Don't. Use external modules with the compiler flag --module commonjs instead : https://www.youtube.com/watch?v=KDrWLMUY0R0&hd=1

If you are still not convinced here are more reasons : https://github.com/TypeStrong/atom-typescript/blob/master/docs/out.md

1 Comment

Thanks for your response. I watched your video and read your post, but still don't know how to achieve what I would like. I would like to define a variable/unknown number of Controllers that can all extend the ControllerBase. I want them to live in separate files to allow small focused files but have them all live under the same module('namespace'). Is this achievable? It seems from your post that --out would be the approach I would have to take, but it isn't recommended because you can't guarantee build order and that could cause lots of issues. Is there any way around this?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.