Currently in the process of transitioning to Typescript with Cypress, I've encountered an issue regarding type casting aliases. While I am anticipating a result of type string
, Typescript is expecting JQuery<HTMLElement>
.
Here's an example:
cy.wrap("a string").as("myString")
cy.get("@myString").then( myString => {
console.log(typeof myString) // => "string"
})
After inspecting the typescript definitions for cy.get
, I discovered the following:
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>>
The current error message reads as follows:
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'.
Any suggestions on how to rectify this error would be greatly appreciated.
Thank you.