Search code examples
typescriptnpm-package

Adding types to JS npm package


I have built a JS package and now i want to add compatibility for typescript. I could not really get it to work by reading the TS docs on how to publish a npm package.

using

import { ClientConfigurationOptions } from "my-package";

is not working.

This is my package's structure:
enter image description here

and this is my package.json file

{
  "name": "my-package",
  "version": "1.0.0",
  "description": "description here",
  "main": "index.js",
  "keywords": [
    "package-name"
  ],
  "author": "iKingNinja",
  "license": "ISC",
  "dependencies": {
    "node-fetch": "^2.6.7"
  },
  "types": "types/index.d.ts"
}

and this is package/types/index.d.ts

export interface ClientConfigurationOptions {
    property1: string,
    property2: number
}

Why do i get the following error?

Module '"my-package"' has no exported member 'ClientConfigurationOptions'.

Solution

  • You have to declare your module in type declaration files for a module:

    declare module "my-package" {
      export interface ClientConfigurationOptions {
        property1: string,
        property2: number
      }
    }
    

    See ambient modules in the handbook.