合并打字稿外部全局接口和继承
Merging typescript external global interfaces and inheritance
在暴露问题之前,我所指的项目在这里:https://github.com/abauzac/nightwatch-typescript
我的问题是 Nightwatch 定义,它在全局导出了很多接口(不在命名空间或模块内 https://github.com/DefinitelyTyped/DefinitelyTyped/blob/38bd4efd5dc8c666f70d77b020a0b64a13ce3980/types/nightwatch/index.d.ts),包括:
./node_modules/@types/nightwatch:
export interface NightwatchCustomCommands { /* empty interface */ }
export interface NightwatchCustomAssertions { /* empty interface */ }
export interface NightwatchBrowser extends NightwatchCustomCommands, NightwatchCustomAssertions, ... { ... }
export interface NightwatchAssertions extends NightwatchBrowser { ... }
我已将自定义命令和断言添加到 Nightwatch 并尝试合并 NightwatchCustomCommands 和 NightwatchCustomAssertions:
./types/index.d.ts
import { NightwatchAssertions, NightwatchBrowser } from "nightwatch";
// merge interfaces with nightwatch types
interface NightwatchCustomAssertions {
compareScreenshot(this: NightwatchBrowser, filename: string, expected: number, callback: Function);
}
interface NightwatchCustomCommands {
wplogin(this: NightwatchBrowser, callback?: Function):this;
compareScreenshot(this: NightwatchBrowser, filename: string, expected?: number, callback?: Function)
}
但是编译的时候好像接口没有合并:
Property 'wplogin' does not exist on type 'NightwatchBrowser'.
Property 'compareScreenshot' does not exist on type 'NightwatchAssertions'.
@types 和 types 文件夹都包含在 tsconfig "typeRoots" 中。到目前为止,我尝试将 "export" 添加到接口、命名空间...不知道我遗漏了什么。
我没有检查过这个,但我认为你必须用模块声明包围它:
import * as NW from "nightwatch";
declare module "nightwatch" {
export interface NightwatchCustomAssertions {
compareScreenshot(this: NW.NightwatchBrowser, filename: string, expected: number, callback: Function): any;
}
export interface NightwatchCustomCommands {
wplogin(this: NW.NightwatchBrowser, callback?: Function):this;
compareScreenshot(this: NW.NightwatchBrowser, filename: string, expected?: number, callback?: Function): any;
}
}
在暴露问题之前,我所指的项目在这里:https://github.com/abauzac/nightwatch-typescript
我的问题是 Nightwatch 定义,它在全局导出了很多接口(不在命名空间或模块内 https://github.com/DefinitelyTyped/DefinitelyTyped/blob/38bd4efd5dc8c666f70d77b020a0b64a13ce3980/types/nightwatch/index.d.ts),包括:
./node_modules/@types/nightwatch:
export interface NightwatchCustomCommands { /* empty interface */ }
export interface NightwatchCustomAssertions { /* empty interface */ }
export interface NightwatchBrowser extends NightwatchCustomCommands, NightwatchCustomAssertions, ... { ... }
export interface NightwatchAssertions extends NightwatchBrowser { ... }
我已将自定义命令和断言添加到 Nightwatch 并尝试合并 NightwatchCustomCommands 和 NightwatchCustomAssertions:
./types/index.d.ts
import { NightwatchAssertions, NightwatchBrowser } from "nightwatch";
// merge interfaces with nightwatch types
interface NightwatchCustomAssertions {
compareScreenshot(this: NightwatchBrowser, filename: string, expected: number, callback: Function);
}
interface NightwatchCustomCommands {
wplogin(this: NightwatchBrowser, callback?: Function):this;
compareScreenshot(this: NightwatchBrowser, filename: string, expected?: number, callback?: Function)
}
但是编译的时候好像接口没有合并:
Property 'wplogin' does not exist on type 'NightwatchBrowser'.
Property 'compareScreenshot' does not exist on type 'NightwatchAssertions'.
@types 和 types 文件夹都包含在 tsconfig "typeRoots" 中。到目前为止,我尝试将 "export" 添加到接口、命名空间...不知道我遗漏了什么。
我没有检查过这个,但我认为你必须用模块声明包围它:
import * as NW from "nightwatch";
declare module "nightwatch" {
export interface NightwatchCustomAssertions {
compareScreenshot(this: NW.NightwatchBrowser, filename: string, expected: number, callback: Function): any;
}
export interface NightwatchCustomCommands {
wplogin(this: NW.NightwatchBrowser, callback?: Function):this;
compareScreenshot(this: NW.NightwatchBrowser, filename: string, expected?: number, callback?: Function): any;
}
}