14

In many cases third party type definitions shipped either via a dedicated @types/* package or alongside the npm package itself are later found to be incomplete or incompatible with a new dependency.

I'm therefore looking for a straightforward way to override the types defined in node_modules. Ideally I'd just be able to copy paste them into my repo and tell the TypeScript compiler to use my types instead.

I've found 3 possible solutions but none are particularly great:

Declare ambient module

declare module 'printer' {
   export function print(msg: string): void;
}

declare module 'printer/color' {
   export function print(msg: string, color: string): void;
}

// WARNING: If you `import` or `export` anything here,
// this will suddenly *augment*, not *override* the
// 'printer' module.
Disadvantages
  • Cannot simply copy and paste the broken types from node_modules because
    • You have to wrap it in declare module 'printer' {}
    • Sub-modules (e.g. import print from 'printer/color') have to be explicitly declared as a separate module declaration.
    • Chances are the original types use import / export and therefore need to be rewritten to work within an ambient module.
  • Can't override existing ambient modules (e.g. declare module 'fs' {} will merely augment types from @types/node not override)

Remap paths in tsconfig

// tsconfig.json
{
  "baseUrl": "./",
  "paths": {
    "printer": ["types/printer"],
    "printer/color": ["types/printer/color"],
  }
}
Advantages
  • Can literally just copy paste the types from node_modules verbatim and apply your own fixes.
Disadvantages
  • Sub-modules (e.g. import bar from 'printer/color') have to be explicitly declared as a separate path mapping.
  • Can't override existing ambient modules (e.g. @types/node)

Install local package

// package.json
{
  "dependencies": {
    "printer": "file:./packages/printer"
  }
}

Advantages

  • Doesn't matter how the original types were written, they won't be installed

Disadvantages

  • Although works well for dedicated @types/* packages, it's not suitable for packages which ship their own types as you'll effectively be taking ownership of the runtime code, meaning you'll miss security fixes etc.

The TypeScript docs don't seem to discuss solutions to this common problem but I wondered if anybody has a more robust solution with fewer caveats?

7
  • 2
  • @KarolMajewski correct me if I'm wrong but would that have similar pro's / con's to installing a local package? i.e. you'd be maintaining the entire code not just the types? May 26, 2021 at 9:01
  • Have you looked at typeRoots? typescriptlang.org/tsconfig#typeRoots
    – Aleksey L.
    May 26, 2021 at 9:14
  • @AlekseyL. I did use typeRoots to allow me to declare module 'printer' {} as per above, e.g. "typeRoots": ["./types", "./node_modules/@types"] but, even if I didn't include "./node_modules/@types" in typeRoots, importing the 'printer' module still seems to read from @types/printer or printer/index.d.ts. I think "typeRoots" is purely to pick up ambient modules?... but not 100% sure? May 26, 2021 at 9:33
  • 3
    @riscarrott With patch-package, you can edit only the part you're interested in. You maintain the patch, not the entire library. You can upgrade the library and reuse the same patch, if needed. No need for copy-pasting anything and no risk of falling out of sync. May 26, 2021 at 15:05

1 Answer 1

0

If you only need to make small changes to a library, the patch-package library offers the best solution. It allows you to make the desired changes directly to your file(s) in node_modules and with a simple command: yarn patch-package @types/package-name generate a patch for that package that is automatically installed for anyone installing the dependencies.

When you update to a new version of your @types-library, patch-package will automatically try to reapply your patch. If there are no conflicts, you won’t have to do anything. If there are conflicts, you either need to reapply your changes and generate a new patch, or hurray, your patch may no longer be needed.

No other solution short of making a PR at the source is as little work and as robust.

Having said that, I personally try to avoid libraries where this is needed as it usually indicates they’re abandoned or poorly supported, and so I haven’t had to patch anything in a long time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.