如何制作一个使用全局类型但不使用全局类型扩展项目的NPM软件包? [英] How to make an NPM package that uses global types but doesn't augment the project using it?

查看:165
本文介绍了如何制作一个使用全局类型但不使用全局类型扩展项目的NPM软件包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何在许多不同文件中重复使用许多类型的项目都可以利用脚本文件中定义的类型.这些类型在整个项目中都是全局可见的,不需要导入,请参见官方手册:

Any project that reuses a lot of types in many different files can make use of types defined in a script file. Those types are visible globally across the project and don't need to be imported, see the official handbook:

在TypeScript中,就像在ECMAScript 2015中一样,任何包含顶级importexport的文件都被视为模块.相反,没有任何顶级importexport声明的文件将被视为脚本,其内容在全局范围内可用(因此也可以在模块中使用).

In TypeScript, just as in ECMAScript 2015, any file containing a top-level import or export is considered a module. Conversely, a file without any top-level import or export declarations is treated as a script whose contents are available in the global scope (and therefore to modules as well).

我在项目中使用了这些全局类型,并且它们运行良好.但是后来我决定将项目变成NPM模块.该模块的全局"类型仅对模块本身可见.

I've used these global types in my project and they worked fine. But then I decided to turn the project into a NPM module. The "global" types of that module should only be visible for the module itself.

首先,我按原样按原样使用该模块,而不添加任何<reference/>typeRoots.但是,当将模块导入另一个项目时,TypeScript无法找到全局类型,也无法编译.

First I pusblished the module as it was, without adding any <reference/>s and typeRoots. However when imported the module into a different project, TypeScript couldn't find the global types and wouldn't compile.

然后,将<reference path="types.ts" />添加到使用全局类型的模块中的所有文件中,然后再次推送.令我惊讶的是,这不仅使模块可以使用类型,而且对使用它的整个项目也可用,从而导致本地类型与模块类型之间发生冲突.

Then I added <reference path="types.ts" /> to all files in the module that used the global types, then I pushed again. To my surprise, this made the types available not only to the module, but also to the whole project that used it, causing conflicts between local types and the types of the module.

(请注意,我发布的模块的两个版本都可以完全自行编译,当我在其他项目中使用npm i安装模块时,问题就开始了.)

(Note that both versions of the module that I published were perfectly compillable on their own, the problems began when I installed the module using npm i in a different project.)

有什么方法可以使模块的全局"类型仅在模块中可用?

.
├── dist
│   ├── index.d.ts
│   ├── index.js
│   ├── foo.d.ts
│   ├── foo.js
│   ├── types.d.ts
│   └── types.js
├── package.json
├── src
│   ├── index.ts  ← contains import/export
│   ├── foo.ts    ← contains import/export
│   └── types.ts  ← contains only types, no import/export
└── tsconfig.json

对于第一次发布,index.tsfoo.ts仅包含常规代码-导致类型在项目时无法识别.当我第二次发布该模块时,我在index.tsfoo.ts中都添加了///<reference path="types.ts"/>.在编译的文件index.d.tsfoo.d.ts中将其转换为path="types.d.ts".

For the first publish, index.ts and foo.ts contained only regular code – that caused the the types not to get recognized when the project. When I published the module for the second time, I added ///<reference path="types.ts"/> to both index.ts and foo.ts. This got translated into path="types.d.ts" in the compiled files index.d.ts and foo.d.ts.

{
  "name": "@foo/foo",
  "version": "1.0.0",
  "main": "dist/index.js"
}

tsconfig.json:

{
  "include": ["./src"],
  "compilerOptions": {
    "moduleResolution": "classic",
    "outDir": "./dist",
    "module": "commonjs",
    "declaration": true
  }
}

推荐答案

如果依赖项需要某些类型,我觉得消费者也应该获取这些类型,因此实现此目标可能并不容易.

If a dependency needs some types, I feel like consumers should get those types, too, so achieving this may not be easy.

您可以尝试在单独的库中定义全局类型(请参阅您的

You could try defining your global types in a separate library (refer to your previous question for ways to do that; this time the library could be as simple as my-global-library/index.d.ts, unless you need to import typings from sibling dependencies – in which case you may consider spinning those off into a shared library of their own), then installing that library as a devDependency of your NPM package. Any project that consumes your NPM package would then not install the devDependency.

这篇关于如何制作一个使用全局类型但不使用全局类型扩展项目的NPM软件包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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