Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

153
Visualizações
How to attach data type to Cypress.as alias function

I have an object out of which I create an alias named userId

cy.wrap(response.id).as('userId');

When referencing userId its type is JQuery<HTMLElement>

cy.get('@userId').then(userId => // userId type is JQuery<HTMLElement> });

How does one define the alias type when defining the alias?

Intention is to have it as a number directly instead of default JQuery<HTMLElement>

EDIT

I do not want something like this

cy.get<number>('@userId').then(userId => // userId type is number });

I want to define the type at function definition.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

The problem is with these two type defns in cypress.d.ts

// cypress.d.ts

/**
 * Get one or more DOM elements by selector.
 * The querying behavior of this command matches exactly how $(…) works in jQuery.
 * @see https://on.cypress.io/get
 * @example
 *    cy.get('.list>li')    // Yield the <li>'s in <.list>
 *    cy.get('ul li:first').should('have.class', 'active')
 *    cy.get('.dropdown-menu').click()
 */
get<E extends Node = HTMLElement>(
  selector: string, 
  options?: Partial<Loggable & Timeoutable & Withinable & Shadow>)
  : Chainable<JQuery<E>>

/**
 * 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>

You can "fix" it by reversing the order,

// cypress/support/index.ts

declare global {
  namespace Cypress {
    interface Chainable<Subject> {
      // change the order of these two dfns
      get<S = any>(alias: string, options?: Partial<Loggable & Timeoutable & Withinable & Shadow>): Chainable<S>
      get<E extends Node = HTMLElement>(selector: string, options?: Partial<Loggable & Timeoutable & Withinable & Shadow>): Chainable<JQuery<E>>
    }
  }
}

Now cy.get('@alias') types to any (because of get<S = any>), which is valid since aliases store any type.

But also the selector version is getting typed to any as well, it seems the identical type signatures make one of the defns redundant.

As Mikhail Bolotov points out, a different command name solves the issue, but I would define it as a custom command in /cypress/support/index.ts and call it getAlias since it has to work for any type.

about 4 years ago · Juan Pablo Isaza Relatório

0

AFAIK, it's impossible to derive the type from alias definition to the alias reference. But you can define it directly on the reference (works in a Typescript file only):

cy.get<number>('@userId').then(userId => // userId type is number });

You also can extract a plain function (or add a custom Cypress command) for such a number alias:

function cyNumber(alias: string) : Chainable<number> {
  return cy.get<number>(alias)
}

cyNumber('@userId').then(userId => // userId type is number });
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda