Skip to content

TypeScript documentation needs clarification #4043

@TheDutchCoder

Description

@TheDutchCoder

Current behavior

The docs say that you can extend the Chainable interface for your custom commands, however this assumes that people use the outdated namespace declarations and linters will complain about it.

Furthermore, it prevents you from importing (external) types, as TS will throw an error on the Chainable Type 'Chainable' is not generic.ts(2315).

Desired behavior

The docs should mention that instead of using this:

// in cypress/support/index.ts
// load type definitions that come with Cypress module
/// <reference types="cypress" />

import type { MyType } from './types'

declare namespace Cypress {
  interface Chainable {
    /**
     * Custom command to select DOM element by data-cy attribute.
     * @example cy.dataCy('greeting')
     */
    dataCy(value: MyType): Chainable<Element> // This will throw a TS error.
  }
}

People should be using this:

// in cypress/support/index.ts
// load type definitions that come with Cypress module
/// <reference types="cypress" />

import type { MyType } from './types'

declare global {
  namespace Cypress {
    interface Chainable {
      /**
       * Custom command to select DOM element by data-cy attribute.
       * @example cy.dataCy('greeting')
       */
      dataCy(value: MyType): Chainable<Element> // This works
    }
  }
}

Test code to reproduce

Use typescript-eslint with default settings and the documentation example will not work with imported types.

Cypress Version

8.0.0

Other

It might be worth looking into ditching namespaces, as it's no longer recommended to use them by TS. They advise to use import/export.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions