扩展打字稿变量声明
Extending a typescript variable declaration
我想在我的 Typescript 项目中使用 nconf-yaml 插件,但我不知道如何将它添加到我的输入中。在@types/nconf中,formats
变量声明如下:
export declare var formats: {
json: IFormat;
ini: IFormat;
};
如何使用declaration merging添加yaml,使其变成:
export declare var formats: {
json: IFormat;
ini: IFormat;
yaml: IFormat
};
我认为你做不到。你不能修改这个变量的类型,因为它是内联的!如果单独声明接口,这是可能的。因此,如果需要,您可以向他们的仓库提交拉取请求 :D
如果 nconf
类型是这样定义的:
export interface IFormats {
json: IFormat;
ini: IFormat;
}
export declare var formats: IFormats;
您会创建一个文件 index.d.ts
,其中包含:
import * as nconf from "nconf";
declare module "nconf" {
export interface IFormats {
json: nconf.IFormat;
ini: nconf.IFormat;
yaml: nconf.IFormat;
}
}
它会解决你的问题。
在此期间,您仍然可以在使用变量之前强制转换它以忽略类型:
(nconf.formats as any).yaml
我想在我的 Typescript 项目中使用 nconf-yaml 插件,但我不知道如何将它添加到我的输入中。在@types/nconf中,formats
变量声明如下:
export declare var formats: {
json: IFormat;
ini: IFormat;
};
如何使用declaration merging添加yaml,使其变成:
export declare var formats: {
json: IFormat;
ini: IFormat;
yaml: IFormat
};
我认为你做不到。你不能修改这个变量的类型,因为它是内联的!如果单独声明接口,这是可能的。因此,如果需要,您可以向他们的仓库提交拉取请求 :D
如果 nconf
类型是这样定义的:
export interface IFormats {
json: IFormat;
ini: IFormat;
}
export declare var formats: IFormats;
您会创建一个文件 index.d.ts
,其中包含:
import * as nconf from "nconf";
declare module "nconf" {
export interface IFormats {
json: nconf.IFormat;
ini: nconf.IFormat;
yaml: nconf.IFormat;
}
}
它会解决你的问题。
在此期间,您仍然可以在使用变量之前强制转换它以忽略类型:
(nconf.formats as any).yaml