首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >类型‘(v1: number,callback: Function) => Promise<void>’不能赋值给类型为'string | ((arguments_0: number) => Promise<void>)‘的参数

类型‘(v1: number,callback: Function) => Promise<void>’不能赋值给类型为'string | ((arguments_0: number) => Promise<void>)‘的参数
EN

Stack Overflow用户
提问于 2021-08-07 07:17:41
回答 1查看 65关注 0票数 2

从wdio更新到wdio (6.11.0 with "typescript":"^4.2.2")后,我开始在browser.executeAsync(runCheck,v1)上看到以下错误

代码语言:javascript
运行
复制
error TS2345: Argument of type ‘(v1: number, callback: Function) => Promise<void>' 
is not assignable to parameter of type 'string | ((arguments_0: number) => void)'.

下面是我的代码:

代码语言:javascript
运行
复制
const runCheck = async (
  v1: number,
  callback: Function
): Promise<void> => {
...
...
...
  callback(v1);
};


const canReceive = (
  browser: WebdriverIO.BrowserObject,
): boolean => {
  const v1 = 50;
   const rate = browser.call(() =>
      browser.executeAsync(runCheck, v1)
    );
  return rate > 10; 
};

我已经尝试过修改

代码语言:javascript
运行
复制
Promise<void> to Promise<any>

但似乎没什么帮助。任何关于如何解决这个问题的观点都是值得感谢的。

提前谢谢你。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-08-08 17:25:52

TLDR,我认为wdio 6中的typescript类型定义是错误的。

尝试以any身份键入runCheck以解决此问题。

代码语言:javascript
运行
复制
import { expect } from 'chai';
import { Browser } from 'webdriverio'

describe('WebdriverIO 6', () => {
    it("has broken typescript type definitions", () => {
        const runCheck: any = 
            async (v1: number, callback: Function): Promise<void> => {
                callback(v1);
            };


        const canReceive = 
            async (browser: WebdriverIO.BrowserObject): Promise<boolean> => {
                const v1 = 50;
                const rate = await browser.call(() => browser.executeAsync(runCheck, v1));
                return rate > 10; 
            };
    })
})

长版本。

我看了一下browser.executeAsync的文档,看起来你在传递它应该接受的参数。

“脚本参数以函数体的形式定义要执行的脚本。将使用提供的参数数组调用该函数,并且可以通过参数对象以指定的顺序访问值。最后一个参数将始终是一个回调函数,必须调用该函数以通知脚本已完成。”https://v6.webdriver.io/docs/api/browser/executeAsync.html

在检查executeAsync的类型定义时,我看到了以下内容

代码语言:javascript
运行
复制
        // there is no way to add callback as last parameter after `...args`.
        // https://github.com/Microsoft/TypeScript/issues/1360
        // executeAsync: <T>(script: string | ((...arguments: any[], callback: (result: T) => void) => void), ...arguments: any[]) => Promise<T>;
        /**
         * Inject a snippet of JavaScript into the page for execution in the context of the currently selected frame.
         * The executed script is assumed to be asynchronous and must signal that is done by invoking
         * the provided callback, which is always provided as the final argument to the function. The value
         * to this callback will be returned to the client.
         */
        executeAsync: <U extends any[], V extends U>(script: string | ((...arguments: V) => void), ...arguments: U) => Promise<any>;
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68690193

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档