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

Correct types for functions stored in useState #28688

Closed
wants to merge 1 commit into from

Conversation

totalolage
Copy link

@totalolage totalolage commented Mar 30, 2024

Summary

Using a function initialiser to a useState hook which was typed to take a function as state could result in the value not being typed as optional where in reality it is.

Example:

// No TS error despite this not being correct. Should error with `void is not assignable to ()=>void`
// instead `value` is typed as `()=>void` but actually has the value `undefined`.
const [value, setValue] = useState<()=>void>(() => {}); 

How did you test this change?

I used the following to validate that my type NotFunction detects all function types (both objects with call signatures and Function instances) correctly: typescript playground link

I validated that the erroneous type assignment is fixed by my changes in this ts playground.
Screenshot 2024-03-31 at 0 32 44

@react-sizebot
Copy link

react-sizebot commented Mar 30, 2024

Comparing: 5d4b758...4d951ea

Critical size changes

Includes critical production bundles, as well as any change greater than 2%:

Name +/- Base Current +/- gzip Base gzip Current gzip
oss-stable/react-dom/cjs/react-dom.production.min.js = 177.14 kB 177.14 kB = 55.07 kB 55.07 kB
oss-experimental/react-dom/cjs/react-dom.production.min.js = 172.98 kB 172.98 kB = 53.93 kB 53.93 kB
facebook-www/ReactDOM-prod.classic.js = 592.80 kB 592.80 kB = 103.99 kB 103.99 kB
facebook-www/ReactDOM-prod.modern.js = 574.49 kB 574.49 kB = 101.02 kB 101.02 kB
test_utils/ReactAllWarnings.js Deleted 64.93 kB 0.00 kB Deleted 16.24 kB 0.00 kB

Significant size changes

Includes any change greater than 0.2%:

Expand to show
Name +/- Base Current +/- gzip Base gzip Current gzip
test_utils/ReactAllWarnings.js Deleted 64.93 kB 0.00 kB Deleted 16.24 kB 0.00 kB

Generated by 🚫 dangerJS against 4d951ea

Copy link

github-actions bot commented Jul 1, 2024

This pull request has been automatically marked as stale. If this pull request is still relevant, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize reviewing it yet. Your contribution is very much appreciated.

@github-actions github-actions bot added the Resolution: Stale Automatically closed due to inactivity label Jul 1, 2024
Copy link

github-actions bot commented Jul 8, 2024

Closing this pull request after a prolonged period of inactivity. If this issue is still present in the latest release, please ask for this pull request to be reopened. Thank you!

@github-actions github-actions bot closed this Jul 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed Resolution: Stale Automatically closed due to inactivity
3 participants