在cypress.d.ts 中有interface Chainable 包含get(alias) 语法的此定义
/**
* Get one or more DOM elements by alias.
* @see https://on.cypress.io/get#Alias
* @example
* // Get the aliased ‘todos’ elements
* cy.get('ul#todos').as('todos')
* //...hack hack hack...
* //later retrieve the todos
* cy.get('@todos')
*/
get<S = any>(alias: string, options?: Partial<Loggable & Timeoutable & Withinable & Shadow>): Chainable<S>
由于某种原因,它没有在测试中出现,但您可以对其进行修补
/// <reference types="cypress" />
// Add type definition in test file or /cypress/support/index.ts
declare namespace Cypress {
interface Chainable {
get<S = any>(alias: string, options?: Partial<Loggable & Timeoutable & Withinable & Shadow>): Chainable<S>
}
}
it('sees the get(alias) type def', () => {
cy.fixture("example").as("details") // using example fixture installed by Cypress
cy.get("@details").then((user) => {
const name: string = user.name // no error
const email: string = user.email // no error
})
})
如果您只想键入一个通用对象,请使用Cypress.ObjectLike,在cypress.d.ts 中定义为
interface ObjectLike {
[key: string]: any
}
在测试中用作
/// <reference types="cypress" />
cy.get<Cypress.ObjectLike>("@details").then((user) => {
const name: string = user.username;
})
注意
别名的赛普拉斯 dfn 具有通用类型 get<S = any>,因此您的解决方案 .then((users: any) => 几乎是等效的。