简体   繁体   中英

Typescript Cypress error when getting variable as string

I'm migrating to using Typescript with Cypress but have an issue type casting aliases. I'm expecting a string but typescript is expecting JQuery<HTMLElement> .

Example:

cy.wrap("a string").as("myString")

cy.get("@myString").then( myString => {
   console.log(typeof myString) // => "string"
})

I checked the typescript definitions for cy.get and found:

get<E extends Node = HTMLElement>(selector: string, options?: Partial<Loggable & Timeoutable & Withinable & Shadow>): Chainable<JQuery<E>>
// AND
get<E extends Node = HTMLElement>(selector: string, options?: Partial<Loggable & Timeoutable & Withinable & Shadow>): Chainable<JQuery<E>>

At the moment, I'm getting the following error:

Argument of type '(myString: string) => string' is not assignable to parameter of type '(this: ObjectLike, currentSubject: JQuery<HTMLElement>) => void'.
      Types of parameters 'myString' and 'currentSubject' are incompatible.
        Type 'JQuery<HTMLElement>' is not assignable to type 'string'.

How can I fix this error?

Thanks.

You can specify the output in the following manner:

cy.get<string>("@myString").then( myString => {
   // Do something here
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM