I have multiple repositories that share similar functionality. I want to export type declarations to an NPM package so I can easily install and use them in my projects.
Within the root directory, there is a folder called /declarations
, containing several *.d.ts
files with code snippets I want to expose. For example:
// declarations/maps.d.ts
declare interface LatLng {
lat: number;
lng: number;
}
declare interface MapMarker {
id: string;
position: LatLng;
}
I've searched online for information on this process, but there isn't much available and what I found varies greatly. This has raised some questions for me:
- To use these declarations in another project, should I export files with a
*.d.ts
extension or convert them to*.ts
? - If I need to export all files from the declarations folder, do I create an
index.d.ts
(or possiblyindex.ts
) file? - Do I have to compile the source code into plain JavaScript files using commands like
npm run build
orrollup
before importing it into another project, or can I directly import the source code? - When attempting to export declaration files like
maps.d.ts
, TypeScript gives an error saying "maps.d.ts is not a module." Does this mean I need to wrap the files in something like:declare module 'mapsModule' { export interface (...) }
- If not, is a regular
sufficient?export interface MapMaker { id: string }
- I know I need to specify the types by adding a
"types"
property inpackage.json
. Do I also need to indicate the main declaration file within package.json as well, such as: - Should I include another
index.ts
(orindex.d.ts
) file in the root directory where I import from/declarations/index.d.ts
?
Once I understand this process and have working types, my goal is to publish everything to an NPM package for easy installation everywhere. However, that's a step for later. I realize my questions may not be perfectly articulated and I might confuse terms and functionalities, as I'm still learning about TypeScript. Any guidance or resources you can recommend would be greatly appreciated. Thank you in advance for your help.