bqlou
bqlou

Reputation: 111

Merging typescript external global interfaces and inheritance

Before exposing the problem, the project I'm referring is here : https://github.com/abauzac/nightwatch-typescript

My problem is with the Nightwatch definitions, it export a lot of interfaces globally (not inside a namespace or module https://github.com/DefinitelyTyped/DefinitelyTyped/blob/38bd4efd5dc8c666f70d77b020a0b64a13ce3980/types/nightwatch/index.d.ts), including :

./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 { ... }

I have added custom commands and assertions to Nightwatch and tried to merge NightwatchCustomCommands and 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)
}

but it seems the interfaces are not merged when compiling :

Property 'wplogin' does not exist on type 'NightwatchBrowser'.
Property 'compareScreenshot' does not exist on type 'NightwatchAssertions'.

both @types and types folder are included in tsconfig "typeRoots". So far I tried adding "export" to the interfaces, namespacing... Don't know what I'm missing.

Upvotes: 1

Views: 1417

Answers (1)

lilezek
lilezek

Reputation: 7374

I haven't checked this but I think you have to surround that with a module declaration:

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

Upvotes: 2

Related Questions