typescript - type '(v1: number, callback: Function) => Promise' 不能分配给 'string | 类型的参数 ((arguments_0: number) => void)'
问题描述
从 wdio v5 更新到 wdio v6(6.11.0 使用“typescript”:“^4.2.2”)后,我开始在 browser.executeAsync(runCheck, v1) 上收到以下错误
error TS2345: Argument of type ‘(v1: number, callback: Function) => Promise<void>'
is not assignable to parameter of type 'string | ((arguments_0: number) => void)'.
下面是我的代码:
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;
};
我试过修改
Promise<void> to Promise<any>
但似乎没有帮助。任何关于如何解决问题的观点表示赞赏。
先感谢您。
解决方案
TLDR,我认为 wdio 6 中的 typescript 类型定义是错误的。
尝试输入runCheck
asany
以解决问题。
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 的文档,看起来您正在传递它应该接受的参数。
“脚本参数以函数体的形式定义要执行的脚本。该函数将使用提供的 args 数组调用,并且可以通过参数对象以指定的顺序访问值。最终参数将始终是回调必须调用该函数以表示脚本已完成。” https://v6.webdriver.io/docs/api/browser/executeAsync.html
在检查 executeAsync 的类型定义时,我看到了这个
// 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>;
推荐阅读
- sql - 将 Google 脚本中 sql 查询的输出解析为简单数组
- certificate - Apple 的 3/29/21 HTTP/2 证书更改需要哪些根 CA 证书?
- c++ - QT 没有错误或警告,但它不输出任何内容
- python - 在所有 .cpp 文件上运行 cpplint
- flask - 在 SQLAlchemy 中计数相关和过滤
- java - 按字母顺序排序后,如何获得列表中重复值最多的计数?
- docker - VS Code Insiders 错误未处理的拒绝:错误产生 C:\...\docker ENOENT 无限重复
- javascript - 如何将 Selenium geckodriver(使用 Javascript API)连接到手动打开的运行 Firefox 会话?
- static-libraries - 如何让 SCons StaticLibrary 将库文件存储在另一个文件夹中
- regex - 如何在Javascript中的字符串每2个单词后添加“\ x”?