代码之家  ›  专栏  ›  技术社区  ›  bqlou

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

  •  1
  • bqlou  · 技术社区  · 8 年前

    在暴露问题之前,我指的项目是: https://github.com/abauzac/nightwatch-typescript

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

    ./node_模块/@类型/夜视:

    export interface NightwatchCustomCommands { /* empty interface */ }
    export interface NightwatchCustomAssertions { /* empty interface */ }
    
    export interface NightwatchBrowser extends NightwatchCustomCommands, NightwatchCustomAssertions, ... { ... }
    
    export interface NightwatchAssertions extends NightwatchBrowser { ... }
    

    我向Nightwatch添加了自定义命令和断言,并尝试合并NightwatchCustomCommands和NightwatchCustomAssertions:

    ./类型/索引。d、 输电系统

    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“typeroot”中。到目前为止,我尝试将“导出”添加到接口、命名空间。。。不知道我错过了什么。

    1 回复  |  直到 8 年前
        1
  •  2
  •   lilezek    8 年前

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

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