diff --git a/cli/CHANGELOG.md b/cli/CHANGELOG.md index 30c4de1ead4b..b0f8fef5b084 100644 --- a/cli/CHANGELOG.md +++ b/cli/CHANGELOG.md @@ -3,6 +3,10 @@ _Released 7/30/2025 (PENDING)_ +**Features:** + +- Adds ability for cy.task function to have type-safety [#32075](https://github.com/cypress-io/cypress/pull/32075). + **Bugfixes:** - Fixed missing support for setting an absolute path for `component.indexHtmlFile` in `@cypress/webpack-dev-server`. Fixes [#31819](https://github.com/cypress-io/cypress/issues/31819). diff --git a/cli/types/cypress.d.ts b/cli/types/cypress.d.ts index 65f51f96722e..184a0c431958 100644 --- a/cli/types/cypress.d.ts +++ b/cli/types/cypress.d.ts @@ -3,6 +3,20 @@ /// /// +type HasNoType = + (() => T extends X ? 1 : 2) extends + (() => 1) ? true : false; + +type CypressConfig_Data = typeof import('../../../cypress.config'); +type AllTasks_CJS = CypressConfig_Data['CypressTasks']; +type AllTasks_ESM = CypressConfig_Data['default']['CypressTasks']; + +type AllTasks = HasNoType extends true ? AllTasks_ESM : AllTasks_CJS; +type TaskEventNames = keyof AllTasks & string; + +type MyParameter = Parameters[0]; +type MyReturnType = Awaited>; + declare namespace Cypress { type FileContents = string | any[] | object type HistoryDirection = 'back' | 'forward' @@ -2164,7 +2178,23 @@ declare namespace Cypress { * * @see https://on.cypress.io/api/task */ - task(event: string, arg?: any, options?: Partial): Chainable + task( + event: T, + ...myArgs: HasNoType extends true ? + [ + arg?: any, + options?: Partial + ] : Parameters['length'] extends 0 ? + [ + arg?: undefined, + options?: Partial + ] : [ + arg: MyParameter, + options?: Partial + ] + ): Chainable< + HasNoType extends true ? unknown : MyReturnType + > /** * Enables you to work with the subject yielded from the previous command.