understanding-npm | A regularly updating survey of the npm community | Graphics library
kandi X-RAY | understanding-npm Summary
kandi X-RAY | understanding-npm Summary
A regularly updating survey of the npm community.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of understanding-npm
understanding-npm Key Features
understanding-npm Examples and Code Snippets
Community Discussions
Trending Discussions on understanding-npm
QUESTION
I have a question regarding resolving types in my project. So basically I have packageA -> packageB-v1 -> packageC-v1
, and I would like to use a type declared in packageC-v1
within packageA
.
All packages are created by myself, and they are all typescript packages that generate declaration files by setting declaration: true
in tsconfig.json
file, they each expose multiple *.d.ts
files in their dist
folder. There is no corresponding @types/*
package for types.
In this case, how should I import types correctly? So far I've tried:
import SomeType from 'packageB-v1/node_modules/packageC-v1/dist/SomeType'
. This works, but I don't like thatpackageA
needs to know wherepackageC
is installed as it could change depends on package manage tool (npm/yarn), or the install orders (See https://medium.com/learnwithrahul/understanding-npm-dependency-resolution-84a24180901b). I've seen issues that due to different versions ofpackageC
, the types are not the same andtsc
is not ok with it. That could happen when there is another dependency likepackageA -> packageB-v1 -> packageBB-v1 -> packageC-v2
, where npm will installpackageC-v2
instead ofpackageC-v1
underpackageB/node_modules
.- First export the necessary types
SomeType
frompackageB-v1
byexport SomeType from 'packageC-v1'
, then from packageA I canimport SomeType from 'packageB-v1'
. This works too, however, this also meanspackageB-v1
is on the hook to re-export all types from all of its dependencies (there could be many of them) that its consumers may need. That is not usually possible. Also, I've heard re-export may generate different types depends on each case. - In
packageA
'spackage.json
file, add the dependency topackageC-v1
explicitly, even it's actually not directly depend on it. So we could useimport SomeType from 'packageC-v1/SomeType
. Unfortunately, this won't work either as we may have another dependency chain likepackageA -> packageD-v1 -> packageC-v2
. In that case, whichpackageC
version we should install underpackageA
? This approach is bad also because even though typescript won't actually includepackageC
in generated JS bundle frompackageA
for using interfaces only, it may do that for enums.
The last way I haven't try is to create my own @types/packageC-v1
and publish it (and my other ts packages). However if I am writing these packages for a private org, that means we need to maintain an internal types repository, as well as maintain the paired versions of packages and types associate with them. Even if I managed to do that, I can still see many issues with this approach in terms of version mismatch, global declaration conflict, or namescope conflict (it is also true in the DefinetelyTyped/types
approach).
I am not sure if these make sense to you, and really need some bits of advice here.
...ANSWER
Answered 2019-Feb-03 at 19:50Let's start with a triviality: if a package A
needs something from package C
, then C
by definition is a direct dependency of A
.
You say you have a reason for not including C
in dependencies of A
we may have another dependency chain like packageA -> packageD-v1 -> packageC-v2
In this case, if A
uses types from C-v1
, how is this supposed to work? I can see only two possibilities:
A
does not need types fromC
when usingpackageD
. (by the way, why does it needC
types for usingpackageB
then?)C
types did not change, soC
types fromC-v1
are compatible withC-v2
The only solution I can see for both #1 and #2 is to split out types from C
into separate package, for example C-types
, and make it a dev dependency of A
.
The types in C-types
should not be the d.ts
files generated by TypeScript, these should be all the interfaces, types and enums (but not classes, classes are an implementation detail and should stay in C
) from C
, moved out into regular .ts
file included in separate package.
You will have to publish C-types
in npm repo in the same way as C
is published, with generated .d.ts
and empty .js
files, for each version of C
. I don't think having empty .js
files is a problem, but if it is, you can just publish manually written .d.ts
files (however I don't know to type-check them before publishing, without having another package using them). There's no need to publish C-types
through DefinitelyTyped and have them in @types
scope - it's a mere convention, not a requirement. You just have to tell everyone in your org to use C-types
instead of @types/C
.
You say that with this approach
I can still see many issues with this approach in terms of version mismatch, global declaration conflict, or namescope conflict (it is also true in the DefinetelyTyped/types approach)
First things first, having global declarations is strongly discouraged - modules were invented for a reason, and every name used anywhere should be explicitly imported from somewhere.
And if your A
uses types from C
, you will have exactly the same problems with versioning with different versions of C
. Splitting types out of C
will just make you think about these problems beforehand, instead of hoping that things will "just work".
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install understanding-npm
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page