How do you put multiple external modules into the same namespace in TypeScript?

前端 未结 3 2002
误落风尘
误落风尘 2020-12-28 16:40

Say I want to have one class per .ts file. I have two .ts files, that look like so:

export module MyClasses { export class A {} }

and

3条回答
  •  被撕碎了的回忆
    2020-12-28 17:05

    Unfortunately there does not seem to be a perfect solution but this is how I solved it for now:

    File 'Controllers/MainController.ts':

    class MainController {
        ...
    }
    
    export = MainController;
    

    File 'Controllers/SecondController.ts':

    class SecondController {
        ...
    }
    
    export = SecondController;
    

    File 'Controllers/Namespace.ts':

    import MainController = require('./MainController');
    import SecondController = require('./SecondController');
    
    export = { MainController, SecondController }
    

    File 'App.ts' (where the 'namespace' is used)

    import Controllers = require('Controllers/Namespace');
    
    angular.module('app', [])
        .controller('MainController', Controllers.MainController)
        .controller('SecondController', Controllers.SecondController)
    

    This gives you nice intellisense, hides the 400 import statements away and keeps the code where the namespace is actually used pretty clean...

提交回复
热议问题