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
User event element types are more narrow than what is returned by RTS queries #648
Comments
I can confirm this is a problem for TS users. |
Thanks for raising this and providing a PR. ❤️ I'm not sure about this change yet. The typings have not been part of the code before A better looking solution might be to add type parameters to the query typings in type GetByRole = <T extends HTMLElement = HTMLElement>(
container: HTMLElement,
role: ByRoleMatcher,
options?: ByRoleOptions,
) => T
type HTMLTextInputElement = HTMLInputElement & {type: 'text'}
userEvent.paste(screen.getByRole<HTMLTextInputElement>('textbox'), 'foo') But I think the type cast is fine as it flags that there is an assumption in the code. userEvent.paste(screen.getByRole('textbox') as HTMLTextInputElement, 'foo') |
Type casting is not ideal, since it guarantees nothing. A better solution would be to have a way to filter query results by tag name, which would guarantee the query is returning the expected type. In the meantime, it feels broken for user-event functions to be incompatible with react-testing-library queries, if they can't be used without type casting. |
It never guarantees anything, yet we don't apply I agree that providing a filter like our I also see that using TypeScript can be cumbersome sometimes and you might prefer taking a shortcut. |
🎉 This issue has been resolved in version 13.1.5 🎉 The release is available on:
Your semantic-release bot 📦🚀 |
@testing-library/user-event
version:13.1.1Relevant code or config
What you did:
Compiled using TypeScript.
What happened:
Type error
Reproduction repository:
N/A
Problem description:
Elements returned by react-testing-library queries are of type
HTMLElement
but user-event functions take more narrow element types likeHTMLInputElement | HTMLLabelElement
. It looks like the type change was made in @testing-library/user-event v13.Suggested solution:
The type definitions for
userEvent.paste
anduserEvent.upload
should either be relaxed to allowHTMLElement
or query methods should be added that return the more narrow element types. Type casting should not be required.The text was updated successfully, but these errors were encountered: