How do I split my module across multiple files in Typescript with node.js

后端 未结 3 1843
时光说笑
时光说笑 2020-12-28 12:29

It seems that the information on how to actually structure code when writing Typescript is next to non-existent.

I want to make a server in node. It has external dep

3条回答
  •  情话喂你
    2020-12-28 13:15

    For anyone who stumbles across this thread (like me) who are trying to create one barrel file for external module declarations, loretoparisi's answer is the way to go. Here is my use case with example modules. I wanted to have a barrel declaration file (index.d.ts) for all my 3rd party declarations I was writing, but didn't want to put it 6 + module declarations in the same file. So this is what I did.

    I have a folder structure like this:

    src/ 
      index.ts
    

    Example index.ts:

    import WildEmitter from 'wildemitter';
    import SomethingCool from 'somethingcool';
    
    // ... rest of file
    

    I converted my folder structure like this to write declaration files for my imported 3rd party modules:

    src/ 
      index.ts
      /types
        index.d.ts
        /modules
          wildemitter.d.ts
          somethingcool.d.ts
    

    In my src/types/index.d.ts file, I have:

    /// 
    /// 
    

    Then I can keep my module declarations in separate files.

    In src/types/modules/wildemitter.d.ts I have:

    declare module 'wildemitter' {
      // declarations
    }
    

    Hopefully this helps someone!

提交回复
热议问题