简体   繁体   English

如何将我的 TS 声明导出到 NPM package?

[英]How to export my TS declarations to NPM package?

I do have some repositories that share some of the functionality.我确实有一些共享某些功能的存储库。 I'd like to export type declarations (and only those types) to NPM package, so that I can install them and use in couple of my projects.我想将类型声明(并且仅那些类型)导出到 NPM package,以便我可以安装它们并在我的几个项目中使用。

In root I do have /declarations folder, which contains a bunch of *.d.ts files, that contain the parts of the code, I'd like to expose.在根目录中,我确实有/declarations文件夹,其中包含一堆*.d.ts文件,其中包含我想公开的部分代码。 Here's one for example:这是一个例子:

// declarations/maps.d.ts

declare interface LatLng {
  lat: number;
  lng: number;
}

declare interface MapMarker {
  id: string;
  position: LatLng;
}

I have been googling this, but there is not so much literature on this, and what is available seems to be really different from each other.我一直在用谷歌搜索这个,但是没有太多关于这个的文献,而且可用的似乎彼此之间真的不同。 In relation to this, I do have some questions:关于这一点,我确实有一些问题:

  1. In order to install and use them in other project, should I export files with *.d.ts extension or convert them to *.ts ?为了在其他项目中安装和使用它们,我应该导出扩展名为*.d.ts的文件还是将它们转换为*.ts
  2. I understand, that in order to export all the files from declarations folder, I should create an index.d.ts (or maybe index.ts ) file?我明白,为了从 declarations 文件夹中导出所有文件,我应该创建一个index.d.ts (或者可能index.ts )文件?
  3. Do I even need to compile the source-code with some sort of command like npm run build or rollup into plain javascript files first?我是否需要先使用某种命令(例如npm run buildrollup到普通 javascript 文件)来编译源代码? Or maybe is it enough if I just import the source code into my other project directly?或者如果我直接将源代码导入到我的其他项目中就足够了吗?
  4. When I try to export declaration files from maps.d.ts , the Typescript is complaining that "maps.d.ts is not a module".当我尝试从maps.d.ts导出声明文件时,Typescript 抱怨“maps.d.ts 不是模块”。 Does it mean that I need to wrap all the files into something like: declare module 'mapsModule' { export interface (...) }这是否意味着我需要将所有文件包装成如下内容: declare module 'mapsModule' { export interface (...) }
  5. If not, will regular export interface MapMaker { id: string } is enough?如果没有,常规的export interface MapMaker { id: string }就足够了吗?
  6. I understand that to I need to "tell" the compiler to look at types by adding "types" property in package.json ?我知道我需要通过在package.json中添加"types"属性来“告诉”编译器查看类型? Id. ID。 est. indicate the main declaration file in my package.json file as well by saying: est. 在我的 package.json 文件中指明主要声明文件,并说:
  7. Do I need to add another index.ts (or index.d.ts ) file in my root directory where I import stuff from /declarations/index.d.ts ?我是否需要在我从/declarations/index.d.ts导入内容的根目录中添加另一个index.ts (或index.d.ts )文件?

My plan, after I figure it out and have working types, is to publish everything to NPM package so I can install it everywhere with ease, but that is only next step.我的计划是,在我弄清楚并拥有工作类型之后,将所有内容发布到 NPM package 这样我就可以轻松地在任何地方安装它,但这只是下一步。

I understand that the questions I am having are not perfectly structured and I may confuse terms and functionality but please note that I am not too experienced with Typescript yet.我知道我遇到的问题结构不完善,我可能会混淆术语和功能,但请注意,我对 Typescript 还不是很熟悉。 I would even appreciate some tips pointing me to the literature where I could educate about the given topic myself.我什至会感激一些提示,这些提示将我指向我可以自己就给定主题进行教育的文献。 Thanks for your answers in advance.提前感谢您的回答。

As per this How to configure custom global interfaces (.d.ts files) for TypeScript?按照这个How to configure custom global interfaces (.d.ts files) for TypeScript? , and most popular answer here by Daniel Tabuenca, I found out that: ,以及丹尼尔·塔布恩卡 (Daniel Tabuenca)在这里最受欢迎的回答,我发现:

"Magically available interfaces" or global types is highly discouraged and should mostly be left to legacy. “神奇可用的接口”或全局类型是非常不鼓励的,应该主要留给遗留物。 Also, you should not be using ambient declaration files (egdts files) for code that you are writing.此外,您不应为正在编写的代码使用环境声明文件(egdts 文件)。 For code you write you should be using plain.ts files to define your interfaces and types.对于您编写的代码,您应该使用 plain.ts 文件来定义您的接口和类型。

That does not solve my problem entirely, but at least there is a good clue on what could cause it.这并不能完全解决我的问题,但至少有一个很好的线索可以说明是什么原因造成的。

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

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