Navos
Navos

Reputation: 67

How to save a type from @types in a node module

My problem stems from the following scenario:

I am working on package A in Typescript. I have written package B in Typescript as well, which will be added as a dependency in package A.

Package B makes use of two javascript modules (C & D) and directly imports the types from their respective @types libraries (@types/C and @types/D).

While package B can build because it has the @types in the dev dependencies, package A will not as it will complain it has no definition of C and D in its own dependencies.

Is there any way to have it so that A will build, but with the following constraints:

  1. A will not have the types of C and D as dev dependencies
  2. B will not include the types as non-dev dependencies
  3. noImplicitAny has to be set to true in ts-config

What Im hoping for is a way to either declare new types in B that are basically copies of the types in C and D, or have A somehow not care about the the compilation issues in its node modules.

Upvotes: 0

Views: 64

Answers (1)

Tyler Church
Tyler Church

Reputation: 669

If package B is truly an independent package, then you should only include the compiled .js files and .d.ts files from B in A (see the --declaration flag in TypeScript's compiler options if you aren't currently generating .d.ts files). This should avoid TypeScript trying to find typings for C and D.

However, if the interface you're exposing from B includes types from C and D, then things get a little more tricky. You can either:

  • Have B declare it's own (equivalent) interfaces as part of its interface, which is correct, since you're hiding C and D from A. C and D are being treated as irrelevant implementation details of B, as far as A is concerned.
  • Concede that A actually does need to know about C and D, and include those as part of its dev dependencies.

Upvotes: 1

Related Questions