Using namespace spread over multiple module files in TypeScript

后端 未结 5 930

I\'ve started work on a large-scale typescript project.

Right from the outset, I want to keep my files organized (this project will be split between lots of developers

5条回答
  •  终归单人心
    2021-01-31 08:46

    Use re-exporting to create an external module that groups and exposes types from other modules:

    // Classes/Animals.ts
    export * from '.\Animals\Mammals';
    export * from '.\Animals\Reptiles';
    

    Then import the types from the new module as usual:

    // app.ts
    import * as Animals from '.\Classes\Animals'
    
    let dog: Animals.Dog;
    let snake: Animals.Snake;
    

    Or

    // app.ts
    import { Dog, Snake } from '.\Classes\Animals'
    
    let dog: Dog;
    let snake: Snake;
    

提交回复
热议问题