Skip to content

Add generics to get/query methods #615

Closed
@nstepien

Description

@nstepien

Describe the feature you'd like:

I know the element type I'm querying, so it'd be beneficial to specify it upfront:

screen.queryByText<HTMLButtonElement>('click me'); // -> HTMLButtonElement | null

Type assertions are very error prone, so should be avoided:

screen.queryByText('click me') as HTMLButtonElement; // can still be null!

Suggested implementation:

queryByText<T extends HTMLElement = HTMLElement>(...) => T | null;

Describe alternatives you've considered:

You can do already do something similar with for example querySelector:

document.body.querySelector<HTMLButtonElement>('.my-button');

Teachability, Documentation, Adoption, Migration Strategy:

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