Search code examples
typescriptdefinitelytyped

Working around a bad index.d.ts file provided by npm package


I recently encountered an npm package that wasn't written in TypeScript but did provide its own index.d.ts file. Unfortunately, these types were inaccurate and contributed by someone who wasn't maintaining the package. When I add a /// <reference... directive to point to my fixed types, VS Code seems unsure of what to do and informs me that there are two type definitions for the given objects. I can't move on while this is unresolved. What's my move here to get working other than editing the file in node_modules on my machine?

If these types were provided by DefinitelyTyped, this would be easy: I'd just remove the bad ones, submit a PR with my changes, use my local version in the meantime. Since this index.d.ts is part of the library itself, is my only option to clone the whole repo, work from that, submit a PR when I'm ready, hope they merge and release a new version quickly? That doesn't seem right. Is there a way to tell the compiler to ignore a specific index.d.ts file in node_modules so mine is the only one?


Solution

  • The comment here, linked in the comment by @AlesD on the other answer, did the trick perfectly. In src, create node_modules/problem-library/index.d.ts and place updated type definitions there. They will override those provided by the library.

    Your .gitignore is likely to ignore your file, which is no good. You can fix that by modifying your .gitignore

    node_modules/
    !src/node_modules/
    

    The first is your existing exclusion of the folder, the second adds a pattern that will negate it for that case.