jonnyknowsbest jonnyknowsbest - 2 months ago 10
TypeScript Question

How to split large typescript index.d.ts file

Our application has many objects and such like that we can roughly break down into 3 categories; 'core', 'app' and 'objects'.

I followed a basic Typescript tutorial online and currently there is a single index.d.ts which has this kind of layout:

interface AH {

}

declare var ah: AH;

declare module 'ah' {
export = ah;
}

declare module ah {

module objects {
// much objects
}

module app {
//many app
}

module core {
//such core
}
}


However due to the archaic source control we use, it would be beneficial to break this single file out to 3 separate files for app, core and objects, having the "namespaces" available as ah.app.blah, ah.core.blah and ah.objects.blah.

How can I achieve this?

Answer

If you are not using this library in another project, there is no need the declare modules.

It seems like you have two options here:

  1. Export the type declarations you want to expose in separate files, and import them when you need them. As this seems to be the most common practice, also when you are handling external modules, I think this has my personal preference.

    app.d.ts (~ core.d.ts, objects.d.ts)

    export interface theAppInterface {}
    

    src/index.ts (example usage)

    import { theAppInterface } from '../app'
    import { theCoreInterface } from '../core'
    
    let appUsage: ah.app.theAppInterface
    
  2. Declare the separate categories in global declaration files, so they're thown into the global namespace and you don't have to import them separately everywhere. Eg.

    app.d.ts (~ core.d.ts, objects.d.ts)

    declare namespace ah {
      namespace app {
        interface theAppInterface {}
      }
    }
    

    src/index.ts

    let appUsage: ah.app.theAppInterface
    let coreUsage: ah.core.theCoreInterface
    
Comments