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

Open
wants to merge 1 commit into
base: main
Choose a base branch
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

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

Successfully merging this pull request may close these issues.

None yet

3 participants