合并typescript外部全局接口和继承

时间:2017-11-25 21:54:50

标签: typescript typescript-typings

在揭露问题之前,我所指的项目在这里:https://github.com/abauzac/nightwatch-typescript

我的问题在于Nightwatch定义,它全局导出了很多接口(不在命名空间或模块https://github.com/DefinitelyTyped/DefinitelyTyped/blob/38bd4efd5dc8c666f70d77b020a0b64a13ce3980/types/nightwatch/index.d.ts内),包括:

./ node_modules / @类型/ 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:

./类型/ 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"接口,命名空间......不知道我错过了什么。

1 个答案:

答案 0 :(得分:2)

我没有检查过这个,但我认为你必须用模块声明来包围它:

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;
    }
  }