在Typescript中编写NPM模块(Writing NPM modules in Typescript)

编程入门 行业动态 更新时间:2024-10-05 07:16:17
在Typescript中编写NPM模块(Writing NPM modules in Typescript)

我正在研究我的第一个NPM模块。 我简要地使用了typescript之前,一个大问题是,对于许多模块,没有定义文件可用。 所以我认为在我的模板中写下我的模块是个好主意。

但是,我找不到任何有关这方面的最佳方式的信息。 我发现这个相关问题“ 我可以在coffeescript中写npm包 ” 吗?人们建议只发布javascript文件。 但是与coffeescript文件相反,如果在打字机应用程序中使用这些文件,那么typescript文件可能会很有用。

发布NPM模块时应该包括Typescript文件,还是应该只发布javascript文件并将生成的.d.ts文件提供给DefinitelyTyped?

I am working on my first NPM module. I briefly worked with typescript before and a big problem was that for many modules there were no definition files available. So I thought it would be a good idea to write my module in typescript.

However, I can't find any information on the best way to do that. I found this related question "Can I write npm package in coffeescript?" where people suggest only publishing the javascript files. But in contrast to the coffeescript files, the typescript files might actually be useful if they are used within a typescript application.

Should I include Typescript files when publishing an NPM module, or should I only publish the javascript files and provide the generated .d.ts files to DefinitelyTyped?

最满意答案

以下是以TypeScript编写的Node模块示例: https : //github.com/basarat/ts-npm-module

以下是使用此示例模块的示例TypeScript项目https://github.com/basarat/ts-npm-module-consume

基本上你需要:

用commonjs和declaration:true编译declaration:true 生成一个.d.ts文件

接着

让你的ide读取生成的.d.ts 。

Atom-TypeScript只是提供了一个很好的工作流: https : //github.com/TypeStrong/atom-typescript#packagejson-support

Here is a sample Node module written in TypeScript : https://github.com/basarat/ts-npm-module

Here is a sample TypeScript project that uses this sample module https://github.com/basarat/ts-npm-module-consume

Basically you need to :

compile with commonjs and declaration:true generate a .d.ts file

And then

Have your ide read the generated .d.ts.

Atom-TypeScript just provides a nice workflow around this : https://github.com/TypeStrong/atom-typescript#packagejson-support

更多推荐

typescript,文件,files,模块,电脑培训,计算机培训,IT培训"/> <meta name="descri

本文发布于:2023-08-07 16:57:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465236.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模块   NPM   Typescript   Writing   modules

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!