简体   繁体   English

NPM模块+ TypeScript最佳实践

[英]NPM Modules + TypeScript Best Practices

i want to write a npm module in TypeScript. 我想在TypeScript中编写一个npm模块。 Can someone recommend me a best practice guide how to get started? 有人可以向我推荐最佳实践指南,如何开始吗?

My Questions are: 我的问题是:

  • Node.js don't support TypeScript out of the box, right? Node.js不支持开箱即用的TypeScript,对吗? So how is the recommended way to publish a npm module? 那么,推荐的发布npm模块的方式是什么? Make 2 Folders /ts /js and reference in the package.json to js/index.js as entry point. 在文件夹/ ts / js中创建2个文件夹,并在package.json中引用js / index.js作为入口点。 Or use some kind of a runtime transpiler and run the TypeScript file directly? 还是使用某种运行时编译器并直接运行TypeScript文件? Or is it not recommended anyway to publish any TypeScript Code in a npm module and just compile it down to normal Javascript and provide the TypeScript source in a Git repository? 还是不建议您在npm模块中发布任何TypeScript代码,然后将其编译为普通Javascript并在Git存储库中提供TypeScript源代码?

  • What is the recommended way for Typing Support? 建议的打字支持方式是什么? I saw i can reference in my package.json to the Typing Files. 我看到我可以在package.json中引用“键入文件”。 Can they be online, should i provide them with the npm module or should ts and dtd files not be published at all? 它们可以在线吗,我应该为他们提供npm模块还是应该完全不发布ts和dtd文件? Install them with typings and provide the typings.json as well? 为它们安装types并提供types.json?

  • If i want to provide typing support for my code how is the recommended way to do this? 如果我想为我的代码提供打字支持,推荐的方式是什么? even my modules are very small and simple (do one thing and do it right) it could be useful to provide some information and autocompletion support. 甚至我的模块也非常小巧(做一件事并正确执行),提供一些信息和自动完成支持可能很有用。 I'm still new to TypeScript so not sure whats the best way to do this. 我仍然是TypeScript的新手,所以不确定执行此操作的最佳方法是什么。 I would guess writing a Interface and provide it with my npm module. 我猜想写一个接口,并提供我的npm模块。 Should these files later be uploaded to the DefinitelyTyped directory or does this only for larger libraries sense? 这些文件以后应该上传到DefinitelyTyped目录,还是仅对较大的库有意义?

I would love to here how to do it correctly with the current TypeScript and Node Version since lot of the informations i found are outdated referring to stuff like autodts, tsd and so on. 我想在这里介绍如何使用当前的TypeScript和Node Version正确执行此操作,因为我发现的许多信息已经过时,涉及诸如autodts,tsd之类的东西。

Im very glad if someone can help me. 我很高兴有人能帮助我。 As soon i know how this is done the right way i will make a documentation and provide these information for others on Github. 我一知道方法是如何正确完成的,我就会制作一个文档,并在Github上为其他人提供这些信息。

Cheers 干杯

To your first question: Yes, you have to transpile the typescript code before you can publish you npm package but you can do this very easily by making sure you have this in your package.json: 第一个问题:是的,您必须先转换打字稿代码,然后才能发布npm软件包,但是通过确保在package.json中包含此代码,可以非常轻松地完成此操作:

  "scripts": {
    "prepublish": "tsc",
  }

Best practice for structering the files is to use eg a src directory for your ts files and then the root level or another directory (eg dist) for the js files. 构造文件的最佳实践是对ts文件使用src目录,然后对js文件使用根目录或其他目录(例如dist)。 You can set this up by defining the "outDir" in your tsconfig.json: 您可以通过在tsconfig.json中定义“ outDir”来进行设置:

"compilerOptions": {
    [...]
    "outDir": "dist"
}

Also the links in the accepted answer here helped me with similar questions: Writing NPM modules in Typescript 同样,这里接受的答案中的链接也帮助我解决了类似的问题: 用Typescript编写NPM模块

As for your question about the typing in the npm package: For providing the typing in the current typescript version you don't need to change anything in your package.json. 关于您在npm软件包中键入的问题:为了在当前的打字稿版本中提供键入,您无需在package.json中进行任何更改。 You can just add a file called index.d.ts to the root level of your package. 您可以仅将一个名为index.d.ts的文件添加到包的根目录下。 If somebody uses your module typescript will automatically find the typing there. 如果有人使用您的模块打字稿,则会在此处自动找到键入内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM