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?
*. 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.
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.
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.
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:
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With