2017-11-25 151 views
1

之前暴露的问题,我指该项目是在这里:https://github.com/abauzac/nightwatch-typescript合并打字稿外部全局接口和继承

我的问题是夜巡定义,它在全球(不出口了大量的接口,在命名空间里或模块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和类型的文件夹都包含在tsconfig “typeRoots”。到目前为止,我尝试添加“导出”到接口,命名空间......不知道我错过了什么。

+0

我不能告诉实际上是什么代码问题的一部分,什么是你尝试过的东西和被遗弃。如果您只显示您正在尝试的代码,我可以提供帮助。例如,在第二个代码块中,你甚至没有做任何继承,所以我不知道你的意图是什么。 – 2017-11-25 23:25:24

+0

我正在尝试合并接口,如第二部分所述:https://www.typescriptlang.org/docs/handbook/declaration-merging.html。我期待看到我的接口(第二块)在继承的接口(第一块)中被考虑到 – bqlou

回答

1

我没有检查这一点,但我认为你必须围绕与一个模块声明:

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

仍然得到完全相同的错误...但是,谢谢我没有尝试这一个。 – bqlou

+0

@bqlou固定。您应该将该代码放在'.d.ts'文件中。 – lilezek

+0

非常感谢,标记为已解决:) – bqlou