以下Cypress命令在Cypress 8.x.x 中编译
declare global {
namespace Cypress {
interface Chainable<Subject> {
login(): Chainable<Token>;
}
}
}
Cypress.Commands.add('login', () => {
return new Cypress.Promise((resolve, reject) => {
// rest
});
});
但是在升级到Cypress9.x.x之后,我得到了以下TypeScript错误:
argument of type '() => Bluebird<unknown>' is not assignable to parameter of type '(person?: Partial<Person>) => Chainable<Token>'.
Type 'Bluebird<unknown>' is missing the following properties from type 'Chainable<Token>': and, as, blur, check, and 83 more.ts(2345)
问题是实现与类型不匹配:实际返回的值(new Cypress.Promise(…)
(是promise,或者更严格地说是PromiseLike
,但不是Token
1。
由于Cypress使用Bluebird-promise,为了匹配实现,login()
的返回类型也必须是Bluebird-promise;你可以从Cypress包中导入这样的符号:
import type Bluebird from "cypress/types/bluebird";
declare global {
namespace Cypress {
interface Chainable {
login(): Bluebird<Token>;
}
}
}
试试2
1–FWIW,OP没有透露Token
是什么,所以从技术上讲,它可以是PromiseLike
,在这种情况下,我的答案是无用的,但IMO这不太可能。在任何情况下,用户@PeaceAndQuiet的相应问题都被忽略了,所以我必须假设Token
不是PromiseLike
(可能是访问令牌(
2–注意cypress
可能不会得到";拾取";出于某种原因,在操场旁;不过它是npm安装的(转到插件→向下滚动(
9.0.0中出现了一些突破性的变化。请参阅9.0.0的变更日志可能会影响你的是排名第六的。它是针对特征17496的。
你应该用能接受蓝鸟承诺的东西来代替<Token>
。或者将其设置为<any>
。
declare global {
namespace Cypress {
interface Chainable<Subject> {
login(): Chainable<any>;
}
}
}
TypeScript错误是由于9.0.0版本中的一个中断更改引起的。参见变更日志:
自定义命令实现现在是基于声明的自定义可链表进行类型化的。地址#17496。
这意味着现在您在实现中的返回值必须与声明的打字员/自定义可链接项相匹配。
在您的情况下,在实现中,您将返回一个Bluebird承诺:
return new Cypress.Promise((resolve, reject) => { ... });
您现在应该更新您的打字界面以匹配类型,可以是:
declare global {
namespace Cypress {
interface Chainable {
login(): Cypress.Promise<Token>;
}
}
}
或者,按照@Dima Parzhitsky的建议:
import type Bluebird from 'cypress/types/bluebird';
declare global {
namespace Cypress {
interface Chainable {
login(): Bluebird<Token>;
}
}
}