Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 681
Jonathan Smith Avatar asked Sep 04 '16 20:09

Jonathan Smith


People also ask

What is index D TS TypeScript?

*. d. ts files are used to provide typescript type information about a module that's written in JavaScript, for example, underscore / lodash / aws-sdk. This will allow you to use the javascript modules without the need to convert them to ts without getting any type of error on your code.

What is Typings D TS?

The typings (or types ) field points to the declaration file ( . d. ts ) that will be used by the TypeScript compiler to understand the API of the package instead of the main file.

What is TypeScript D TS file?

d. ts files are declaration files that contain only type information. These files don't produce . js outputs; they are only used for typechecking. We'll learn more about how to write our own declaration files later.


1 Answers

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
    
like image 116
Pelle Jacobs Avatar answered Sep 22 '22 09:09

Pelle Jacobs