Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

152
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!