node.jstypescriptnpmtsconfigtype-definition

Creating and importing private package of hand-written type definitions


I need to create a private npm package containing (only) custom type definitions (typedefs). These are hand-written d.ts files that were not generated by TypeScript. As these are proprietary, I can't add them to DefinitelyTyped.

Folder structure: typings / mymodule / index.d.ts
index.d.ts contains: declare module mymodule { ... }

The package preserves the structure, so after installing in another project I have:
node_modules / @mycompany / mytypes / typings / mymodule / index.d.ts

Unfortunately now I have to edit tsconfig.json in the other project to add:

    "include": [
      "node_modules/@mycompany/mytypes/typings"
    ],

or optionally (and more correctly?) add under compilerOptions:

      "typeRoots": [
        "node_modules/@types",
        "node_modules/@mycompany/mytypes/typings"
      ]

After that, it works - I can reference mymodule.SomeType in the other project.

Is there a way I can make this work automatically on npm install without having to edit tsconfig includes (that is, have it behave like a DefinitelyTyped package)?

If there is a more correct way to do this, that's a valid answer.

I'm using TypeScript 3.6 or 3.7, depending on the importing project.


Solution

  • Is there a way I can make this work automatically on npm install without having to edit tsconfig includes

    The only way to make this work without any changes to tsconfig.json is if the typings are shipped as a part of the module you are trying to use.

    E.g. this shipping as a part of module will occur automatically if you write your package in TypeScript.