将外部模块 TypeScript 声明暴露给消费模块 [英] Exposing external module TypeScript declarations to consuming modules

查看:53
本文介绍了将外部模块 TypeScript 声明暴露给消费模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个已发布的 TypeScript 模块(我们称之为 shared-stuff),旨在由其他 TypeScript 模块导入.这个 shared-stuff 模块有第三方依赖,没有 @types 范围的声明,所以在这个模块里面有几个声明文件:

/lib/declarations/东西.d.ts另一件事.d.ts

这些声明文件在 shared-stuff 的上下文中工作正常.但是,一旦消费应用程序开始从 shared-stuff 导入,TypeScript 就会出现如下错误:

<块引用>

找不到模块another-thing"的声明文件.

我可以通过让消费者从依赖项中显式导入 .d.ts 文件来解决这个挑战,但这并不理想,因为每个消费者都必须做同样的事情.

有没有办法让消费模块继承"依赖项的声明?

解决方案

由于您的使用者(shared-stuff 的使用者)依赖于 another-thing 的类型,您也需要导出它们.

一种方法是在您的 index.ts 中使用 ///(并记住包含您的 /lib/declarations 在您的发行版中.

另一种方法是不依赖外部类型.即,而不是做:

import { SomeType } from 'another-thing'导出函数 foo(): SomeType { ... }

自己定义该类型(在 shared-stuff 中,而不是在 another-thing.d.ts 中):

导出类型 SomeType = { ... }导出函数 foo(): SomeType { ... }

理论上,another-thing 中的类型应该遵循语义版本作为库本身,但实际上它更容易发生破坏性变化.

一个原因是该库一开始不是用 TypeScript 编写的,因此库作者可能会在没有意识到的情况下意外破坏类型.

因此,虽然与重用类型相比,自己声明类型听起来很脆弱,但实际上并非如此.(在您的情况下,您还是自己定义它们).

只要确保你有一套好的测试来捕捉任何类型的破坏性变化.

I have a published TypeScript module (let's call it shared-stuff) that is designed to be imported by other TypeScript modules. This shared-stuff module has third-party dependencies that have no @types-scoped declarations, so inside of that module there are several declaration files:

/lib/declarations/
  something.d.ts
  another-thing.d.ts

These declaration files work fine within the context of shared-stuff. However, once the consuming app starts importing from shared-stuff, TypeScript gives me errors like:

Could not find a declaration file for module 'another-thing'.

I can work around this challenge by having the consumer explicitly import the .d.ts files from the dependency, but this is not ideal since every consumer would have to do that same thing.

Is there a way to have a consuming module "inherit" the declarations from a dependency?

解决方案

Since your consumer (consumer of shared-stuff) relies on the typings of another-thing, you need to export them too.

One way to do that is using /// <reference> in your index.ts (and remember to include your /lib/declarations in your distribution.

Another way is don't rely on the external typings. i.e., instead of doing:

import { SomeType } from 'another-thing'
export function foo(): SomeType { ... }

Define that type yourself (inside shared-stuff, instead of in another-thing.d.ts):

export type SomeType = { ... }
export function foo(): SomeType { ... }

In theory, the types in another-thing should follow semantic version as the library itself, but in reality it is much more prone to breaking changes.

One reason is that the library was not written in TypeScript to begin with, so the library author may accidentally break the types without aware of it.

Therefore, while declaring the types yourself sound fragile comparing to reusing type, it really isn't. (and in your case you are defining them yourself anyway).

Just make sure you have a good suite of tests to catch any type breaking changes.

这篇关于将外部模块 TypeScript 声明暴露给消费模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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