I do have some repositories that share some of the functionality. I'd like to export type declarations (and only those types) to NPM package, so that I can install them and use in couple of my projects.
In root I do have /declarations
folder, which contains a bunch of *.d.ts
files, that contain the parts of the code, I'd like to expose. Here's one for example:
// declarations/maps.d.ts
declare interface LatLng {
lat: number;
lng: number;
}
declare interface MapMarker {
id: string;
position: LatLng;
}
I have been googling this, but there is not so much literature on this, and what is available seems to be really different from each other. In relation to this, I do have some questions:
- In order to install and use them in other project, should I export files with
*.d.ts
extension or convert them to*.ts
? - I understand, that in order to export all the files from declarations folder, I should create an
index.d.ts
(or maybeindex.ts
) file? - Do I even need to compile the source-code with some sort of command like
npm run build
orrollup
into plain javascript files first? Or maybe is it enough if I just import the source code into my other project directly? - When I try to export declaration files from
maps.d.ts
, the Typescript is complaining that "maps.d.ts is not a module". Does it mean that I need to wrap all the files into something like:declare module 'mapsModule' { export interface (...) }
- If not, will regular
export interface MapMaker { id: string }
is enough? - I understand that to I need to "tell" the compiler to look at types by adding
"types"
property inpackage.json
? Id. est. indicate the main declaration file in my package.json file as well by saying: - Do I need to add another
index.ts
(orindex.d.ts
) file in my root directory where I import stuff from/declarations/index.d.ts
?
My plan, after I figure it out and have working types, is to publish everything to NPM package so I can install it everywhere with ease, but that is only next step.
I understand that the questions I am having are not perfectly structured and I may confuse terms and functionality but please note that I am not too experienced with Typescript yet. I would even appreciate some tips pointing me to the literature where I could educate about the given topic myself. Thanks for your answers in advance.