Skip to content

typescript declaration for ToHaveValue doesn't allow null #240

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
timrobinson33 opened this issue May 5, 2020 · 4 comments
Closed

typescript declaration for ToHaveValue doesn't allow null #240

timrobinson33 opened this issue May 5, 2020 · 4 comments

Comments

@timrobinson33
Copy link

  • @testing-library/jest-dom version: 5.5.0
  • node version: 12.16.2
  • npm (or yarn) version: 6.14.4
  • react-testing-library version: (if applicable) 10.0.4

Relevant code or config:

expect(inputElement).toHaveValue(null);

==> typescript warning

What you did:

I'm using <input type="number"> to prompt for an optional numeric value. It seems the obvious way to handle an empty value is to use null , and certainly React doesn't give any warnings when you try to do this.

What happened:

Typescript gives a warning because the method signature for toHaveValue doesn't allow null. using the non-null assertion operator (i.e. expect(inputElement).toHaveValue(null!); works fine and is the best workaround IMO.

It might be possible to set an empty string to indicate an empty value, but this would require me to declare my state variable as number | string | null which is definitely not desirable

Suggested solution:

From what I can tell there are already some tests calling it with null (just google toHaveValue null and they come up at the top of the list), so I presume it is valid to do this and you just need to update the typescript declarations.

@gnapse
Copy link
Member

gnapse commented May 6, 2020

Yes, I agree. Though the type definitions are not in this repo, but in DefinitelyTyped. Not sure what the process is there, if we also open an issue there, or just the PR (if you're up for it that's cool BTW). But I'm fine to keep this issue here to keep track of this, because ultimately is an issue with our lib, even if we do not provide the types right here (I wish we did).

@timrobinson33
Copy link
Author

OK thanks. I'm new to contributing but since it looks like you've agreed it's a fault, and it should be simple to fix, it looks like a good one to cut my teeth on. I'll try a PR on DefinitelyTyped and reference this issue.

@timrobinson33
Copy link
Author

I see the fix has been released in https://www.npmjs.com/package/@types/testing-library__jest-dom/v/5.0.4. As far as I'm concerned, you can close this issue now

@eps1lon
Copy link
Member

eps1lon commented May 10, 2020

Fixed in DefinitelyTyped/DefinitelyTyped#44541

@eps1lon eps1lon closed this as completed May 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants