admin管理员组文章数量:1287246
I crafted some classes and modules in TypeScript. Other TypeScript apps can use the TS files directly without needing type definitions. However, to publish to npm, I guess I need to publish both JS files and the type definition files. I think it could be wasteful to hand-craft d.ts files, so How to generate type definition files from TypeScript files?
I crafted some classes and modules in TypeScript. Other TypeScript apps can use the TS files directly without needing type definitions. However, to publish to npm, I guess I need to publish both JS files and the type definition files. I think it could be wasteful to hand-craft d.ts files, so How to generate type definition files from TypeScript files?
Share Improve this question asked Sep 14, 2018 at 5:08 ZZZZZZ 2,8122 gold badges28 silver badges38 bronze badges 1-
4
Just set
"declaration": true
piler option – Aleksey L. Commented Sep 14, 2018 at 6:03
1 Answer
Reset to default 11Short answer
Add "declaration": true
to your tsconfig to have *.d.ts files generated along with *.js files
Long answer
To publish *.js and *.d.ts files to npm but not to github (generally you want to keep only source in your source control) you need to do few things:
- Add
"declaration": true
to tsconfig - Add
"outDir": "dist"
to tsconfig - Add dist to .gitignore
- Add .npmignore like .gitignore but without dist
- Add
"main": "dist/index.js"
in package.json (note the "js" suffix and dist directory) - Add
"types": "dist/index.d.ts"
in package.json (note the "ts" suffix this time and dist directory)
(Your file names and directories may vary of course, the above is assuming that your distribution directory is dist
and your main entry point is index.ts
)
This is without configuring build scripts, just a bare minimum to have published what you need so that other Node modules and programs could require your module using both JavaScript and TypeScript, without keeping generated files under source control.
For more details see:
- https://stackoverflow./a/55609763/613198
This is an answer that I wrote while preparing my talk about Deno (slides, video) trying describe the entire process of publishing Node modules written in TypeScript to npm, which is exactly what you are doing here so maybe you'll find more useful info not only about generating .d.ts files but also other related things.
本文标签: javascriptHow to generate type definition file from TypeScript filesStack Overflow
版权声明:本文标题:javascript - How to generate type definition file from TypeScript files? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741282705a2370099.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论