用 TypeScript 编写 npm 模块
我正在开发我的第一个 npm 模块.我之前曾短暂地使用过 TypeScript,一个大问题是许多模块没有可用的定义文件.所以我认为用 TypeScript 编写我的模块是个好主意.
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.
但是,我找不到任何有关执行此操作的最佳方法的信息.我发现了这个相关的问题我可以在 CoffeeScript 中编写 npm 包吗?"人们建议只发布 JavaScript 文件.但与 CoffeeScript 文件相比,如果在 TypeScript 应用程序中使用 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.
我应该在发布 npm 模块时包含 TypeScript 文件,还是应该只发布 JavaScript 文件并将生成的 .d.ts 文件提供给绝对类型?
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
Here is a sample Node module written in TypeScript : https://github.com/basarat/ts-npm-module
这是一个使用此示例模块的示例 TypeScript 项目 https://github.com/basarat/ts-npm-module-consume
Here is a sample TypeScript project that uses this sample module https://github.com/basarat/ts-npm-module-consume
基本上你需要:
- 使用
commonjs
和declaration:true
编译 - 生成
.d.ts
文件
然后
- 让你的ide阅读生成的
.d.ts
.
Atom-TypeScript 只是提供了一个很好的工作流程:https://github.com/TypeStrong/atom-typescript#packagejson-support
Atom-TypeScript just provides a nice workflow around this : https://github.com/TypeStrong/atom-typescript#packagejson-support