TypeScript. How to rewrite type definition file (file.d.ts) of any node_modules package

Artem Tkachenko
1 min readDec 19, 2017

--

If you have some node_modules package with the very old /odd / bad definition of TypeScript, you can easily rewrite it so your JS code will still go from the node_modules/package, but your type definition will go from the /typings folder.

  1. you have a node_modules/somepackage package(folder and files) with bad SomePackage.d.ts file
  2. in your project folder, in any of your .ts files, you still can make import of this package like this:
import SomePackage from ‘somepackage’;

3. just go to your project typings folder, (project/typings)

4. create somepackage folder with index.d.ts in it (project/typings/somepackage/index.d.ts)

5. Add next code into the somepackage/index.d.ts file:

declare module 'somepackage' {
export interface SomePackageProps {
prop1: string,
prop2: string,
}
interface SomePackage {
hello: string,
worlt: number,
}

export default SomePackage;
}

6. And than you can do in any of your .ts files:

import SomePackage, { SomePackageProps } from ‘somepackage’;

Voila!

--

--

Responses (4)