Skip to content
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

fix(types) Correct overload order for act without strictNullChecks in TypeScript #622

Merged
merged 1 commit into from May 18, 2021

Conversation

andyrooger
Copy link
Contributor

What:

Correct the TypeScript definitions, so that we produce the correct return type for act(...) calls when strictNullChecks is turned off.

Why:

Take the following code

import { act } from '@testing-library/react-hooks'

const result = act(() => Promise.resolve())

With strictNullChecks turned on, the type of result is Promise<undefined> as you would expect.
Turn strictNullChecks off, and the type of result is now void.

This is particularly highlighted in my case as the project I'm working on has eslint rules that will complain if we try to await a value it thinks is not thenable.

How:

It seems the issue is that with strictNullChecks, the two overloads for act(...) do not have a lot of overlap so the order we specify them in doesn't matter. Without strictNullChecks, anything that matches the async overload also matches the sync overload. Since the sync overload is specified first, it is always chosen.

I've just swapped the overloads around so that the more specific one with Promises is checked first.

Checklist:

  • Tests
  • Ready to be merged
  • Added myself to contributors table

@andyrooger
Copy link
Contributor Author

@mpeyper I've followed the contributors doc for this, but it looks like you're using a bot for adding contributors now. Do I need to strip that change out?

@joshuaellis
Copy link
Member

No you don't, the bot is just to help us if someone forgets!

@codecov
Copy link

codecov bot commented May 17, 2021

Codecov Report

Merging #622 (146b04a) into main (18f7913) will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff            @@
##              main      #622   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           15        15           
  Lines          218       218           
  Branches        26        26           
=========================================
  Hits           218       218           

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 18f7913...146b04a. Read the comment docs.

@mpeyper
Copy link
Member

mpeyper commented May 18, 2021

Thanks @andyrooger, I can see there is actually a note about this very issue in react's own types for act.

@mpeyper mpeyper merged commit 09a96d1 into testing-library:main May 18, 2021
@andyrooger
Copy link
Contributor Author

@mpeyper so they do! That could have saved me the long explanation. Thanks for the merge :)

mpeyper added a commit that referenced this pull request May 18, 2021
…622)

There was an issue with a patch release, so this manual-releases.md
change is to release a new patch version.

Reference: 09a96d1
@github-actions
Copy link

🎉 This PR is included in version 5.1.3 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants