Skip to main content

Publishing Type Definitions

It can be useful to be able to publish these types in custom NPM repos e.g. Gitlab

Configuring package.json File

Including Including src and  and dist  folders

use files key in package.json to indicate which directories should be published (https://stackoverflow.com/questions/67523877/index-d-ts-file-not-published-to-npm). By default you likely want to publish both the source code in src and the compiled/transpiled typescript->js files in dist

e.g.

{
  "files": [
    "dist",
    "src"
  ],
}

Including types and main

You also need to include types  property which tells npm where your defined types (index.d.ts) sits - this is likely to be in in dist/index.d.ts  if you follow the conventions in this document.

Main module

In a node module you'd probably use use src/index.js  but we want to use use dist/index.js  - this will be the compiled version of our typescript module.

Configuring tsconfig.json

You probably want something like this:

{
  "compilerOptions": {
      "sourceMap": true,
      "outDir": "dist",
      "strict": true,
      "lib": [
          "esnext"
      ],
      "allowJs": true,
      "declaration": true,
      "esModuleInterop": true
  },
  
  "include": ["src/**/*"],
}

Use dist for our outdir so that the output from transpilation goes to the right place. Use declaration: true to have tsc produce an index.d.tsdeclaration file and plonk it into the dist folder so that it is picked up by the package.json we configured above.