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: useLocalStorage overload for correct undefined behavior #2556

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

mmkal
Copy link

@mmkal mmkal commented Apr 25, 2024

Fixes #2551

Instead of using a single function which accepts defaultValue?: T - which forces a return type of T | undefined, use an overload. When a defaultValue is supplied, the useState-like tuple returned uses T. When no defaultValue is supplied, it's T | undefined.

Runtime usage is exaclty the same, this just improves the types.

TypeScript playground demo of the bug and the fix.

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as before)

Checklist

Note: I tested this in my node_modules folder and the typescript playground, but I pasted into the GitHub web UI, so I'm hoping that CI will catch any regressions. If anything goes red I'll clone locally and fix.

  • Read the Contributing Guide
  • Perform a code self-review
  • Comment the code, particularly in hard-to-understand areas
  • Add documentation
  • Add hook's story at Storybook
  • Cover changes with tests
  • Ensure the test suite passes (yarn test)
  • Provide 100% tests coverage
  • Make sure code lints (yarn lint). Fix it with yarn lint:fix in case of failure.
  • Make sure types are fine (yarn lint:types).

Fixes streamich#2551

Instead of using a single function which accepts `defaultValue?: T` - which forces a return type of `T | undefined`, use an overload. When a defaultValue is supplied, the useState-like tuple returned uses `T`. When no defaultValue is supplied, it's `T | undefined`.

Runtime usage is exaclty the same, this just improves the types.
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

Successfully merging this pull request may close these issues.

useLocalStorage without 'or undefined'
1 participant