Skip to content

testing: add opt-in type-safe cy.task function #32014

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

Closed
wants to merge 14 commits into from
32 changes: 31 additions & 1 deletion cli/types/cypress.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,20 @@
/// <reference path="./cypress-eventemitter.d.ts" />
/// <reference path="./cypress-type-helpers.d.ts" />

type IsAny<X> =
(<T>() => T extends X ? 1 : 2) extends
(<T>() => T extends any ? 1 : 2) ? true : false;

type CypressConfig_Data = typeof import('../../../cypress.config')
type AllTasks_CJS = CypressConfig_Data['CypressTasks']
type AllTasks_ESM = CypressConfig_Data['default']['CypressTasks']

type AllTasks = IsAny<AllTasks_CJS> extends true ? AllTasks_ESM : AllTasks_CJS
type TaskEventNames = keyof AllTasks & string

type MyParameter<T extends TaskEventNames> = Parameters<AllTasks[T]>[0]
type MyReturnType<T extends TaskEventNames> = Awaited<ReturnType<AllTasks[T]>>

declare namespace Cypress {
type FileContents = string | any[] | object
type HistoryDirection = 'back' | 'forward'
Expand Down Expand Up @@ -2164,7 +2178,23 @@ declare namespace Cypress {
*
* @see https://on.cypress.io/api/task
*/
task<S = unknown>(event: string, arg?: any, options?: Partial<Loggable & Timeoutable>): Chainable<S>
task<T extends TaskEventNames>(
event: T,
...myArgs: IsAny<AllTasks> extends true ?
[
arg?: any,
options?: Partial<Loggable & Timeoutable>
] : Parameters<AllTasks[T]>['length'] extends 0 ?
[
arg?: undefined,
options?: Partial<Loggable & Timeoutable>
] : [
arg: MyParameter<T>,
options?: Partial<Loggable & Timeoutable>
]
): Chainable<
IsAny<AllTasks> extends true ? unknown : MyReturnType<T>
>

/**
* Enables you to work with the subject yielded from the previous command.
Expand Down
Loading