Skip to content

useLocalStorageState types: Return T state instead of T | undefined when defaultValue is set #2773

@nh2

Description

@nh2

Currentlym when you pass useLocalStorageState a { defaultValue: "abc" }, the returned "getter" is still of type T | undefined instead of just T:

const [myVal, setMyVal] = useLocalStorageState("my-key", { defaultValue: "abc" });

This is annoying, because it means you cannot use useLocalStorageState()/useSessionStorageState() as a drop-in replacement for React's useState() -- you always have to handle the undefined manually.

But logically, it seems the TypeScript typings are just wrong / less precise than they could be, and if defaultValue is set myVal should just be of type T, is that right?

If yes, could the typings be fixed?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions