How do I use namespaces with TypeScript external modules?

前端 未结 9 1299
陌清茗
陌清茗 2020-11-22 08:59

I have some code:

baseTypes.ts

export namespace Living.Things {
  export class Animal {
    move() { /* ... */ }
  }
  export class          


        
9条回答
  •  隐瞒了意图╮
    2020-11-22 09:44

    Nothing wrong with Ryan's answer, but for people who came here looking for how to maintain a one-class-per-file structure while still using ES6 namespaces correctly please refer to this helpful resource from Microsoft.

    One thing that's unclear to me after reading the doc is: how to import the entire (merged) module with a single import.

    Edit Circling back to update this answer. A few approaches to namespacing emerge in TS.

    All module classes in one file.

    export namespace Shapes {
        export class Triangle {}
        export class Square {}      
    }
    

    Import files into namespace, and reassign

    import { Triangle as _Triangle } from './triangle';
    import { Square as _Square } from './square';
    
    export namespace Shapes {
      export const Triangle = _Triangle;
      export const Square = _Square;
    }
    

    Barrels

    // ./shapes/index.ts
    export { Triangle } from './triangle';
    export { Square } from './square';
    
    // in importing file:
    import * as Shapes from './shapes/index.ts';
    // by node module convention, you can ignore '/index.ts':
    import * as Shapes from './shapes';
    let myTriangle = new Shapes.Triangle();
    

    A final consideration. You could namespace each file

    // triangle.ts
    export namespace Shapes {
        export class Triangle {}
    }
    
    // square.ts
    export namespace Shapes {
        export class Square {}
    }
    

    But as one imports two classes from the same namespace, TS will complain there's a duplicate identifier. The only solution as this time is to then alias the namespace.

    import { Shapes } from './square';
    import { Shapes as _Shapes } from './triangle';
    
    // ugh
    let myTriangle = new _Shapes.Shapes.Triangle();
    

    This aliasing is absolutely abhorrent, so don't do it. You're better off with an approach above. Personally, I prefer the 'barrel'.

提交回复
热议问题