Definitelytyped: [@types/nightmare] Compile error when passing a callback with arguments to .evaluate()

Created on 16 Aug 2018  路  4Comments  路  Source: DefinitelyTyped/DefinitelyTyped

  • [x] I tried using the @types/nightmare package and had problems.
  • [x] I tried using the latest stable version of tsc. https://www.npmjs.com/package/typescript
  • [x] I have a question that is inappropriate for StackOverflow. (Please ask any appropriate questions there).
  • [x] [Mention](https://github.com/blog/821-mention-somebody-they-re-notified) the authors (see Definitions by: in index.d.ts) so they can respond.

    • Authors: @horiuchi @samyang-au @Bleser92

Package versions

Code

Taken straight from Nightmare's .evalute() example usage:

const selector = 'h1'
nightmare
  .evaluate((selector: string) => {
    // now we're executing inside the browser scope.
    return (document.querySelector(selector) as HTMLHeadingElement).innerText
  }, selector) // <-- that's how you pass parameters from Node scope to browser scope
  .then(text => {
    // ...
  })

Issue

I am getting this compile error.

error TS2345: Argument of type '(selector: string) => string' is not assignable to parameter of type '() => string'.

Most helpful comment

I've hit the same issue. The problem is that the type definitions are for Nightmare v2, while the current version is v3. The evaluate signature has changed since v2 - a mandatory callback argument has been removed. This is why TypeScript will not be able to work with this package until it it properly rewritten for v3.

As a temporary measure, I modified the typings, index.d.ts, lines 38-43:

evaluate<T1, T2, T3, R>(fn: (arg1: T1, arg2: T2, arg3: T3) => R, arg1: T1, arg2: T2, arg3: T3): Nightmare;
evaluate<T1, T2, R>(fn: (arg1: T1, arg2: T2) => R, arg1: T1, arg2: T2): Nightmare;
evaluate<T, R>(fn: (arg: T) => R, arg: T): Nightmare;
evaluate<T>(fn: (arg: T) => void, arg: T): Nightmare;
evaluate<R>(fn: () => R): Nightmare;
evaluate(fn: () => void): Nightmare;

So now I can write something like this:

import * as Nightmare from "Nightmare";

const nightmare = new Nightmare({
    show: true
});

const input: number = 0;

(async () => {
  await nightmare.goto("https://github.com/")
    .wait("h1")
    .evaluate<number, string>( // number = argument type, string = output type
      (_input: number) => {
        const title: any = document.querySelectorAll("h1");
        return title[_input].textContent as string;
      },
      input
    )
    .end()
    .then((result: string) => {
      console.log(result);
    });
})();

@horiuchi @samyang-au @Bleser92 Can we expect typings for Nightmare v3 anytime soon?

All 4 comments

@horiuchi, @samyang-au, @Bleser92, any updates on the issue?

@piousdeer I am also running into the same problem. Did you ever find a solution to this?

@JamesRArroyo unfortunately not. I had to uninstall @types/nightmare completely.

I've hit the same issue. The problem is that the type definitions are for Nightmare v2, while the current version is v3. The evaluate signature has changed since v2 - a mandatory callback argument has been removed. This is why TypeScript will not be able to work with this package until it it properly rewritten for v3.

As a temporary measure, I modified the typings, index.d.ts, lines 38-43:

evaluate<T1, T2, T3, R>(fn: (arg1: T1, arg2: T2, arg3: T3) => R, arg1: T1, arg2: T2, arg3: T3): Nightmare;
evaluate<T1, T2, R>(fn: (arg1: T1, arg2: T2) => R, arg1: T1, arg2: T2): Nightmare;
evaluate<T, R>(fn: (arg: T) => R, arg: T): Nightmare;
evaluate<T>(fn: (arg: T) => void, arg: T): Nightmare;
evaluate<R>(fn: () => R): Nightmare;
evaluate(fn: () => void): Nightmare;

So now I can write something like this:

import * as Nightmare from "Nightmare";

const nightmare = new Nightmare({
    show: true
});

const input: number = 0;

(async () => {
  await nightmare.goto("https://github.com/")
    .wait("h1")
    .evaluate<number, string>( // number = argument type, string = output type
      (_input: number) => {
        const title: any = document.querySelectorAll("h1");
        return title[_input].textContent as string;
      },
      input
    )
    .end()
    .then((result: string) => {
      console.log(result);
    });
})();

@horiuchi @samyang-au @Bleser92 Can we expect typings for Nightmare v3 anytime soon?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

variousauthors picture variousauthors  路  3Comments

jbreckmckye picture jbreckmckye  路  3Comments

Loghorn picture Loghorn  路  3Comments

csharpner picture csharpner  路  3Comments

JWT
svipas picture svipas  路  3Comments