Will Huang
Will Huang

Reputation: 3586

How to define types in protractor.conf.js with @ts-check enabled

In my Angular CLI v7.3.6 project, I have a protractor.conf.js file. I'd like to enable @ts-check in this file in my VSCode. When @ts-check is eanbled, I'd like to call browser.getCapabilities() in the onPrepare() callback but VSCode said Unable to find name 'browser'. ts(2304).

The browser should be registered into Global. I tried to use <reference path="..."/> syntax, but no browser has been declared.

/// <reference path="../node_modules/protractor/built/index.d.ts" />

I can't figure out how to declare Global properties with type in a js file (Node.js).

How can I declare browser's type in protractor.conf.js file?

Upvotes: 1

Views: 470

Answers (1)

Will Huang
Will Huang

Reputation: 3586

I finally figure out the solution:

  1. Add the following comment at first line of the protractor.conf.js.

    // @ts-check
    
  2. Add the following local variable and get real value from global, then assign typing to this local variable.

    /**
    * @type { import("protractor").ProtractorBrowser }
    */
    let browser = global['browser'];
    

Here is the screenshot of the usage:

enter image description here

Upvotes: 2

Related Questions