用 TypeScript 编写 npm 模块 [英] Writing npm modules in TypeScript

查看:37
本文介绍了用 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

基本上你需要:

  • 使用 commonjsdeclaration: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

这篇关于用 TypeScript 编写 npm 模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆