无法在 Conf.ts 中声明 onComplete 函数 >> 出现错误——类型 '(passed: any) => void' 不可分配给类型 '() => void'.ts(2322)

Unable to declare onComplete function in Conf.ts >>Getting error -- Type '(passed: any) => void' is not assignable to type '() => void'.ts(2322)

我正在从头开始开发 Protractor - browserstack 框架。

同时使用 onComplete 功能,如 conf.ts 中所述 - https://automate.browserstack.com/dashboard/v2/quick-start/get-started#introduction

 // Code to mark the status of test on BrowserStack based on test assertions
      onComplete: function (passed) {
        if (!passed) {
          browser.executeScript('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed","reason": "At least 1 assertion has failed"}}');
        }
        if (passed) {
          browser.executeScript('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"passed","reason": "All assertions passed"}}');
        }
      }

我在控制台上遇到以下错误

*附加调试器。 conf.ts:87:1 - 错误 TS2322:类型 '(passed: any) => void' 不可分配给类型 '() => void'。 87 onComplete:函数(通过){

node_modules/protractor/built/config.d.ts:410:5
 410     onComplete?: () => void;
         ~~~~~~~~~~
 The expected type comes from property 'onComplete' which is declared here on type 'Config'*



Can someone help me understand how to resolve this error?

这看起来像是 onComplete 类型定义中的错误。在用法中,onComplete 似乎传递了一个变量。参见:https://github.com/angular/protractor/blob/master/lib/frameworks/jasmine.js#L109-L115

const originalOnComplete = runner.getConfig().onComplete;

jrunner.onComplete(async(passed) => {
  try {
    if (originalOnComplete) {
      await originalOnComplete(passed);
    }

尽管您可以创建一个 TypeScript config.ts,但最好创建一个 config.js 文件。当您要引用浏览器对象时,使用 config.ts 也可能 运行 出现问题。必须通过 require 而不是 import 引入浏览器变量。