New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: add missing Cypress.Commands.addAll() types #20894
Changes from 1 commit
79d7afa
d78e899
fff475e
cc73dce
df111ff
2d5fa83
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -24,9 +24,15 @@ declare namespace Cypress { | |||||
interface CommandFn<T extends keyof ChainableMethods> { | ||||||
(this: Mocha.Context, ...args: Parameters<ChainableMethods[T]>): ReturnType<ChainableMethods[T]> | void | ||||||
} | ||||||
interface CommandFnArgs<T extends keyof ChainableMethods> { | ||||||
[name: string]: (this: Mocha.Context, ...args: any) => (ReturnType<ChainableMethods[T]> | void) | ||||||
} | ||||||
interface CommandFnWithSubject<T extends keyof ChainableMethods, S> { | ||||||
(this: Mocha.Context, prevSubject: S, ...args: Parameters<ChainableMethods[T]>): ReturnType<ChainableMethods[T]> | void | ||||||
} | ||||||
interface CommandFnArgsWithSubject<T extends keyof ChainableMethods, S> { | ||||||
[name: string]: (this: Mocha.Context, prevSubject: S, ...args: any) => (ReturnType<ChainableMethods[T]> | void) | ||||||
} | ||||||
interface CommandOriginalFn<T extends keyof ChainableMethods> extends CallableFunction { | ||||||
(...args: Parameters<ChainableMethods[T]>): ReturnType<ChainableMethods[T]> | ||||||
} | ||||||
|
@@ -467,6 +473,14 @@ declare namespace Cypress { | |||||
add<T extends keyof Chainable, S extends PrevSubject>( | ||||||
name: T, options: CommandOptions & { prevSubject: S[] }, fn: CommandFnWithSubject<T, PrevSubjectMap<void>[S]>, | ||||||
): void | ||||||
addAll<T extends keyof Chainable>(args: CommandFnArgs<T>): void | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I named it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After leaving the comment above, I felt that |
||||||
addAll<T extends keyof Chainable>(options: CommandOptions & {prevSubject: false}, fn: CommandFnArgs<T>): void | ||||||
addAll<T extends keyof Chainable, S extends PrevSubject>( | ||||||
options: CommandOptions & { prevSubject: true | S | ['optional'] }, fn: CommandFnArgsWithSubject<T, PrevSubjectMap[S]>, | ||||||
): void | ||||||
addAll<T extends keyof Chainable, S extends PrevSubject>( | ||||||
options: CommandOptions & { prevSubject: S[] }, fn: CommandFnArgsWithSubject<T, PrevSubjectMap<void>[S]>, | ||||||
): void | ||||||
overwrite<T extends keyof Chainable>(name: T, fn: CommandFnWithOriginalFn<T>): void | ||||||
overwrite<T extends keyof Chainable, S extends PrevSubject>(name: T, fn: CommandFnWithOriginalFnAndSubject<T, PrevSubjectMap[S]>): void | ||||||
} | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why
...args: any
instead of...args: Parameters<ChainableMethods[T]>
? Could we re-using the existing CommandFn interface?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At first, I wrote it this way, because
...args: Parameters<ChainableMethods[T]>
doesn't work. TypeScript tries to interpretargs
as the union type of all possiblecy
method arg types (i.e. [type of cy.get parameters] | [type of cy.visit parameters] | etc... ).This happened because
<T extends keyof ChainableMethods>
part defined after theinterface
means that thisCommandFnArgs
interface should use the parameters of one ofChainableMethods
(i.e. one of the name ofcy
methods) for all of its functions. It's not the right behavior.So, I thought about using generics inside
[key]
brackets. I realized that the purpose ofaddAll
is to add a new command. We're not reusing anycy
command. They should beany
, because we don't know what they are. They're defined by users.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gottchya. That makes sense! Thank you for the explanation!