In a typescript project I need to use the bytewise npm module. This package does not have a type definition in the DefinitelyTyped npm namespace, so I can't use the method described in typescript documentation:
npm install --save-dev @types/bytewise
Since there's no type declaration for this module, it is not possible to use it, as tsc
complains with: error TS2307: Cannot find module 'bytewise'
.
To fix the issue, I have written a declaration file for bytewise
and added it to node_modules/@types/bytewise/index.d.ts
, which worked perfectly.
However, this is a tedious common pattern: I use a small npm library for which there are no @types declaration, create a directory under node_modules/@types
, add an index.d.ts
containing the declarations for the library, then git force/add that file(since node_modules
is ignored by default).
Clearly I don't plan to maintain those declarations forever under my private node_modules
directory, eventually I'd like to contribute them back to DefinitelyTyped so they can be installed more easily later, but rather than creating a separate directory/file for each library I'm using, I'd rather have a single vendor.d.ts
file in the root of my project with all declarations not available on npm. Something like this(this is what I have in my project ATM):
import bl = require("bl");
import {Stream} from "stream";
declare module "bytewise" {
export function encode(val: any): bl;
export function decode(val: bl): any;
export const buffer: boolean;
export const type: string;
}
declare module "JSONStream" {
export function parse(patternOrPath?: string): Stream;
export function stringify(open?: string, sep?: string, close?: string);
}
The question is: How do I make these declarations available to all files in my project? I have tried modifying tsconfig.json
to include it before the entry point:
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"noImplicitAny": false,
"removeComments": true,
"preserveConstEnums": true,
"outDir": "out",
"sourceMap": true
},
"files": [
"vendor.d.ts",
"index.ts"
]
}
but still get error TS2307: Cannot find module 'bytewise'.
in the files where I have import * as bytewise from "bytewise";
. I also tried adding a /// <reference path="..." />
comment pointing to vendor.d.ts
but got the same error. Do I really need to have a separate declaration file for each npm module or am I missing something?