pizzarob
pizzarob

Reputation: 12029

How to Type My Own Node Module Using a .d.ts File

I am bundling my node module using webpack and awesome-typescript-loader. I am importing the bundled module into another package. Within the other package I get the error:

Could not find a declaration file for module 'mymodule'

My default export for the package is an initialized class:

import MyClass from './myClass';
const c = new MyClass();
export default c;

and from my package I import:

import c from 'mymodule';

MyClass is typed within its file, but how can I export a .d.ts file from the module, so my other package knows about the MyClass types?

Upvotes: 0

Views: 224

Answers (1)

felixmosh
felixmosh

Reputation: 35493

You can add a types.d.ts file in your lib folder, and add the types: "./types.d.ts" property to its package.json.

This will allow to TS access to all the types that your lib is exporting.

But, you will need to write those types by hand.

A better solution, is to let TS generate those d.ts files for you. You can achieve that by enabling declaration: true in your tsconfig.json file.

Upvotes: 1

Related Questions