2

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:

  1. In order to install and use them in other project, should I export files with *.d.ts extension or convert them to *.ts?
  2. I understand, that in order to export all the files from declarations folder, I should create an index.d.ts (or maybe index.ts) file?
  3. Do I even need to compile the source-code with some sort of command like npm run build or rollup into plain javascript files first? Or maybe is it enough if I just import the source code into my other project directly?
  4. 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 (...) }
  5. If not, will regular export interface MapMaker { id: string } is enough?
  6. I understand that to I need to "tell" the compiler to look at types by adding "types" property in package.json? Id. est. indicate the main declaration file in my package.json file as well by saying:
  7. Do I need to add another index.ts (or index.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.

mlvrkhn
  • 87
  • 1
  • 12

1 Answers1

0

As per this How to configure custom global interfaces (.d.ts files) for TypeScript?, and most popular answer here by Daniel Tabuenca, I found out that:

"Magically available interfaces" or global types is highly discouraged and should mostly be left to legacy. Also, you should not be using ambient declaration files (e.g. d.ts files) for code that you are writing. For code you write you should be using plain .ts files to define your interfaces and types.

That does not solve my problem entirely, but at least there is a good clue on what could cause it.

mlvrkhn
  • 87
  • 1
  • 12