如何修复项目库中损坏的类型签名?
How do you fix a broken type signature in a library for your project?
我在我的项目中使用了 debug
和 @types/debug
包,最近对 @types/debug
的更新破坏了我知道有效代码的输入。
有没有一种方法可以使用声明合并来彻底修复我项目中的键入,这样我就不必等待修复使其上游或复制整个 .d.ts?
@types/debug
的相关部分是这样的:
declare var debug: debug.Debug;
export = debug;
declare namespace debug {
type IDebugger = Debugger;
interface Debugger {
log: (v: any) => string;
}
}
我添加了自己的 .d.ts 文件:
declare module 'debug' {
interface Debugger {
log: (...args: any[]) => any;
}
}
但现在我在尝试使用原始声明文件中的其他成员时遇到错误:“模块 'debug' 没有导出成员”等等。似乎发生的事情是,一旦我添加了自己的 .d.ts 文件,它没有与原始文件合并,但它被用作该模块的唯一声明。为什么会发生这种情况,我该如何做,即正确合并声明?
您不能扩充 log
的签名,因为它被定义为函数类型。如果它被定义为一个接口,则可以通过添加重载来实现:
interface Debugger {
log: Logger;
}
interface Logger {
(...args: any[]): any
(arg: any): string
}
那你能做什么?
- 安装不同版本的 typings 并锁定它(在 运行
npm install
时使用 --save-exact
标志)。
- 在本地覆盖类型并继续使用它们,直到上游的定义得到修复。理想情况下,您自己对 DefinitelyTyped 做出更改。
至于第二种解决方案,由于扩充不是一个选项,您必须重新创建整个定义并添加您的更改。结果可能如下所示:
debug.d.ts
declare module 'debug' {
var debug: debug.Debug & { debug: debug.Debug, default: debug.Debug };
namespace debug {
interface Debug {
(namespace: string): Debugger;
coerce: (val: any) => any;
disable: () => void;
enable: (namespaces: string) => void;
enabled: (namespaces: string) => boolean;
names: RegExp[];
skips: RegExp[];
formatters: Formatters;
}
type IDebug = Debug;
interface Formatters {
[formatter: string]: (v: any) => string;
}
type IDebugger = Debugger;
interface Debugger {
(formatter: any, ...args: any[]): void;
enabled: boolean;
log: Logger
namespace: string;
extend: (namespace: string, delimiter?: string) => Debugger;
}
interface Logger {
(...args: any[]): any
(arg: any): string
}
}
export = debug;
}
这样的声明文件需要包含在你的项目中。
我在我的项目中使用了 debug
和 @types/debug
包,最近对 @types/debug
的更新破坏了我知道有效代码的输入。
有没有一种方法可以使用声明合并来彻底修复我项目中的键入,这样我就不必等待修复使其上游或复制整个 .d.ts?
@types/debug
的相关部分是这样的:
declare var debug: debug.Debug;
export = debug;
declare namespace debug {
type IDebugger = Debugger;
interface Debugger {
log: (v: any) => string;
}
}
我添加了自己的 .d.ts 文件:
declare module 'debug' {
interface Debugger {
log: (...args: any[]) => any;
}
}
但现在我在尝试使用原始声明文件中的其他成员时遇到错误:“模块 'debug' 没有导出成员”等等。似乎发生的事情是,一旦我添加了自己的 .d.ts 文件,它没有与原始文件合并,但它被用作该模块的唯一声明。为什么会发生这种情况,我该如何做,即正确合并声明?
您不能扩充 log
的签名,因为它被定义为函数类型。如果它被定义为一个接口,则可以通过添加重载来实现:
interface Debugger {
log: Logger;
}
interface Logger {
(...args: any[]): any
(arg: any): string
}
那你能做什么?
- 安装不同版本的 typings 并锁定它(在 运行
npm install
时使用--save-exact
标志)。 - 在本地覆盖类型并继续使用它们,直到上游的定义得到修复。理想情况下,您自己对 DefinitelyTyped 做出更改。
至于第二种解决方案,由于扩充不是一个选项,您必须重新创建整个定义并添加您的更改。结果可能如下所示:
debug.d.ts
declare module 'debug' {
var debug: debug.Debug & { debug: debug.Debug, default: debug.Debug };
namespace debug {
interface Debug {
(namespace: string): Debugger;
coerce: (val: any) => any;
disable: () => void;
enable: (namespaces: string) => void;
enabled: (namespaces: string) => boolean;
names: RegExp[];
skips: RegExp[];
formatters: Formatters;
}
type IDebug = Debug;
interface Formatters {
[formatter: string]: (v: any) => string;
}
type IDebugger = Debugger;
interface Debugger {
(formatter: any, ...args: any[]): void;
enabled: boolean;
log: Logger
namespace: string;
extend: (namespace: string, delimiter?: string) => Debugger;
}
interface Logger {
(...args: any[]): any
(arg: any): string
}
}
export = debug;
}
这样的声明文件需要包含在你的项目中。