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

Change getItem's default to undefined #1005

Closed
avi12 opened this issue Sep 27, 2024 · 4 comments
Closed

Change getItem's default to undefined #1005

avi12 opened this issue Sep 27, 2024 · 4 comments
Labels

Comments

@avi12
Copy link

avi12 commented Sep 27, 2024

Is your feature request related to a bug?

N/A

Feature request

Suppose I want to fetch multiple storage values, currently, it'll look like:

const [value1, value2] = await Promise.all([
  storage.getItem<Type1>("local:key1").then(val => val ?? "foo"),
  storage.getItem<Type2>("local:key2").then(val => val ?? "bar")
]);;

I prefer having this code instead:

const [value1 = "foo", value2 = "bar"] = await Promise.all([
  storage.getItem<Type1>("local:key1"),
  storage.getItem<Type2>("local:key2"),
]);

which can be achieved without triggering a TypeScript error by changing null to undefined in

const getFallback = () => opts?.fallback ?? opts?.defaultValue ?? null;

@avi12 avi12 added the feature label Sep 27, 2024
@aklinker1
Copy link
Collaborator

The storage API has a built-in way of providing default values.

const [value1, value2] = await Promise.all([
  storage.getItem<Type1>("local:key1", { fallback: "foo" }),
  storage.getItem<Type2>("local:key2", { fallback: "bar" }),
]);

The storage APIs were designed to feel similar to localStorage, which is why it currently returns null instead of undefined. At this point, it would be a pretty disruptive breaking change to make.

I'm not necessarily opposed to this, undefined does make sense in many ways, but this would be really annoying to migrate to. So many little edge cases and checks (like someone accidentally used === null insetad of == null, etc).

@avi12
Copy link
Author

avi12 commented Sep 27, 2024

Huh, I was not aware of the { fallback: ... } second argument

@avi12
Copy link
Author

avi12 commented Sep 27, 2024

But does it actually affect getItem's return type to get rid of null?

baraich added a commit to baraich/wxt that referenced this issue Sep 28, 2024
@baraich
Copy link
Contributor

baraich commented Sep 29, 2024

@avi12, I have fixed the type issue and made the pull request (9516324) and once merged the issue would resolved in production.

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

No branches or pull requests

3 participants