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

feat: typed storage interface #509

Merged
merged 14 commits into from
Dec 15, 2024
Merged

Conversation

wzc520pyfm
Copy link
Contributor

Refer: #508

We can improve the ts type as follows:

type TestObjType = {
  a: number;
  b: boolean;
};
type MyStorage = {
  foo: string;
  baz: TestObjType;
};
const storage = createStorage<MyStorage>();
await storage.getItem("foo"); // => <string>
await storage.getItem("baz"); // => <TestObjType>
storage.setItem("foo", "val"); // Check ok
storage.setItem("foo", 123); // TS error

and get hints as you type:
image

@pi0 pi0 changed the title type(storage): enhance ts type for get/set/has/del feat: typed storage interface Nov 14, 2024
@wzc520pyfm wzc520pyfm requested a review from pi0 November 29, 2024 16:03
@pi0 pi0 requested a review from danielroe December 10, 2024 14:40
Copy link

codecov bot commented Dec 11, 2024

Codecov Report

Attention: Patch coverage is 94.44444% with 1 line in your changes missing coverage. Please review.

Project coverage is 62.93%. Comparing base (4d61c78) to head (4d6d13b).
Report is 122 commits behind head on main.

Files with missing lines Patch % Lines
src/storage.ts 94.44% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #509      +/-   ##
==========================================
- Coverage   65.05%   62.93%   -2.12%     
==========================================
  Files          39       39              
  Lines        4055     3343     -712     
  Branches      487      579      +92     
==========================================
- Hits         2638     2104     -534     
+ Misses       1408     1236     -172     
+ Partials        9        3       -6     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@danielroe danielroe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really nice to see!

I wonder if we could support extracting keys with : - so having a namespace:child would get the right types with a base of namespace and a key of child....

Copy link
Member

@pi0 pi0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

@pi0 pi0 merged commit 6163be0 into unjs:main Dec 15, 2024
4 checks passed
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.

3 participants