2017-11-25 167 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