How do I consume custom type definitions for an npm module?

357 Views Asked by At

I have written type definitions for the npm package react-sortable-tree. I am unable to figure how I should consume it. Here's what I have tried.

I named the definition file react-sortable-tree/index.d.ts (following directions given in Typescript documentation) and added the filename to my tsconfig.json. The Typescript compiler (version 2.2.1) could not find the module's definition. I tried both module resolution methods Classic and Node without success.

After renaming the file as react-sortable-tree.d.ts, the compiler recognized the file with resolution method Classic. However, there are other modules that come with type definition files included with the npm package (for example, redux), and the compiler fails to find the definition for these packages.

When using the resolution method Node, the compiler finds type definitions for all packages, except the one that I have defined. For the react-sortable-tree package, the compiler resolves the path to the actual Javascript file that comes with the package and ignores my definitions. As a result, no type checking happens.

How do I write my definition, where do I place it, and what compiler options should I specify so that that compiler considers my type definitions?

1

There are 1 best solutions below

2
On

Here's my setup for a declaration file for the npm module jison:

tsconfig:

"include": [
    "./src/definitions/jison.d.ts",
    "./src/index.ts"
 ]

./src/definitions/jison.d.ts:

declare module Jison
{
    export class Parser {
        constructor(grammar:any);
        generate():string;
        parse(program:string):any;
    }
}
declare module "jison"
{
    export = Jison;
}