Compile to .d.ts on publish to npm #6
Replies: 7 comments 13 replies
-
Opinions differe about whether npm package authors should put a There are pros & cons to shipping a JavaScript npm package with a Pros
Cons
As a publisher of JavaScript code to npm I'd rather not break the user experience for the 80% of JS users that dont care about typescript/jsdoc, which is what the existance of For the 20% of users who are using TypeScript / JSDoc, they can already whitelist the JSDoc source code in Maybe the real answer is to publish the artifact twice to npm, once with and once without the |
Beta Was this translation helpful? Give feedback.
-
I forgot to mention what Why? If one has lots of modules that interoperate with one another or if one relies on big complex types like eg I created that module when working at @Sydsvenskan as we for interoperability reasons referenced types from quite a few of our modules and if we had needed to add all those modules as dependencies, then in the end all modules would almost depend on all modules when it was actually only the types that were referenced. We also got some issues with |
Beta Was this translation helpful? Give feedback.
-
Hi All, I have been generating typedefs on release and publishing them with a package. As of recently go to definitions are fixed by typedef maps and typeVersions in In my experience that provides best experience to lib users as they get all the typings in vscode and/or TS projects without having to do any configuration. And jump to definition also take then to the source js instead of .d.ts files Here’s one example of such setup https://github.com/multiformats/js-multiformats/blob/master/package.json |
Beta Was this translation helpful? Give feedback.
-
@voxpelli |
Beta Was this translation helpful? Give feedback.
-
I’ll have to go check what’s up with that, that package was probably not the best example as it also tries to do both ESM & cjs and I suspect that creates problems. |
Beta Was this translation helpful? Give feedback.
-
you can try https://github.com/multiformats/js-multihash go to definition should work as expected |
Beta Was this translation helpful? Give feedback.
-
I can confirm that |
Beta Was this translation helpful? Give feedback.
-
JSDoc types only gets validated in the very project one is in. Any JSDoc types in
node_modules
are ignored by default, only.d.ts
files gets imported by TypeScript by default.While one can make TypeScript pick up JSDoc types in
node_modules
by setting"maxNodeModuleJsDepth"
to a value more than0
, that is both very slow + tends give false errors by picking up JSDoc comments that have been added in code for pure documentation purposes and which haven't been validated in any form.The better way is to always publish
.d.ts
with ones project when one publishes it tonpm
.How?
The way I have typically done it can be seen in eg
async-htm-to-string
andmicropub-express
:Add a
declaration.tsconfig.json
extending yourtsconfig.json
Ignore the generated
.d.ts
in your.gitignore
*.d.ts
Add a build step to your
package.json
"scripts"
:From
micropub-express
build-step usingnpm-run-all
:And the clean step:
Add a
prepublishOnly
step to yourpackage.json
"scripts"
:"prepublishOnly": "run-s build",
This will generate the types when you publish to
npm
Maybe add a
"types"
to yourpackage.json
:"types": "main.d.ts",
wheremain
should be the same as the name on your"main"
field. If not set it will default toindex.d.ts
, but maybe rather be explicit than implicit?Optionally add a manually crafted
.d.ts
, eg.advanced-types.d.ts
for complex types:clean:declarations
torm -rf $(find . -maxdepth 2 -type f -name '*.d.ts' ! -name 'advanced-types.d.ts')
!/advanced-types.d.ts
on a line after*.d.ts
in your.gitignore
npm publish
(or similar if you prefer, maybenp
?)Beta Was this translation helpful? Give feedback.
All reactions