如何将自定义类型定义文件转换为 npm 类型定义文件
How to convert custom type definition file to npm type definition file
我的打字稿项目中有一个用 CoffeeScript 编写的依赖项的自定义模块定义文件。
它看起来像下面这样并且效果很好:
src/@types/dependency-name.d.ts
declare module 'dependency-name' {
import EventEmitter from 'eventemitter3';
/**
* omitted
*/
interface ICable {
new (channels: Record<string, ISubscription>): ICable;
channels: Record<string, ISubscription>;
channel: (name: string) => ISubscription | undefined;
setChannel: (name: string, channel: ISubscription) => ISubscription;
}
export const Cable: ICable;
}
所以我想为这个项目做贡献并提交类型定义文件。
我试过的是:
- 分叉原始 CoffeeScript 项目,
- 将上面的定义复制到
index.d.ts
- 将以下行添加到
pacakge.json
:
"typings": "index.d.ts",
- 在我的打字稿项目中安装了分支存储库以进行测试。
但是,它不识别添加的index.d.ts
文件。显示类型定义丢失错误。
我已经看到 并猜测问题是这个:
be written as external modules
但是我不明白。有人可以指导我将我的自定义类型(我猜是模块扩充吗?)转换为更正 package.json
的类型定义文件?
我建议直接查看从您链接的 post 链接的 TypeScript handbook page,因为 post 本身相当旧,而且那里的文本副本有点过时.特别是,"types"
现在比 package.json
中的 "typings"
更受欢迎,尽管两者应该仍然有效。
但是,您和 post 是正确的,问题是环境外部模块(“环境模块”)与非环境模块(“外部模块”)之一。您应该只需要删除环绕文件的 declare module
块,将其内容保留在顶层。这会将您的声明从环境声明转换为非环境声明,并且应该可以正常工作。
我的打字稿项目中有一个用 CoffeeScript 编写的依赖项的自定义模块定义文件。
它看起来像下面这样并且效果很好:
src/@types/dependency-name.d.ts
declare module 'dependency-name' {
import EventEmitter from 'eventemitter3';
/**
* omitted
*/
interface ICable {
new (channels: Record<string, ISubscription>): ICable;
channels: Record<string, ISubscription>;
channel: (name: string) => ISubscription | undefined;
setChannel: (name: string, channel: ISubscription) => ISubscription;
}
export const Cable: ICable;
}
所以我想为这个项目做贡献并提交类型定义文件。
我试过的是:
- 分叉原始 CoffeeScript 项目,
- 将上面的定义复制到
index.d.ts
- 将以下行添加到
pacakge.json
:
"typings": "index.d.ts",
- 在我的打字稿项目中安装了分支存储库以进行测试。
但是,它不识别添加的index.d.ts
文件。显示类型定义丢失错误。
我已经看到
be written as external modules
但是我不明白。有人可以指导我将我的自定义类型(我猜是模块扩充吗?)转换为更正 package.json
的类型定义文件?
我建议直接查看从您链接的 post 链接的 TypeScript handbook page,因为 post 本身相当旧,而且那里的文本副本有点过时.特别是,"types"
现在比 package.json
中的 "typings"
更受欢迎,尽管两者应该仍然有效。
但是,您和 post 是正确的,问题是环境外部模块(“环境模块”)与非环境模块(“外部模块”)之一。您应该只需要删除环绕文件的 declare module
块,将其内容保留在顶层。这会将您的声明从环境声明转换为非环境声明,并且应该可以正常工作。