Skip to content

Commit

Permalink
fix(types): allow passing generic to untyped storage (#543)
Browse files Browse the repository at this point in the history
* fix(types): allow passing generic to untyped storage

* chore: lint
  • Loading branch information
danielroe authored Dec 26, 2024
1 parent 4a3c7a6 commit 605ec41
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ export interface Storage<T extends StorageValue = StorageValue> {
key: K,
ops?: TransactionOptions
): Promise<StorageItemType<T, K> | null>;
getItem(
getItem<R = StorageItemType<T, string>>(
key: string,
opts?: TransactionOptions
): Promise<StorageItemType<T, string> | null>;
): Promise<R | null>;

/** @experimental */
getItems: <U extends T>(
Expand Down
8 changes: 8 additions & 0 deletions test/storage.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ describe("types", () => {
await storage.getItem("foo")
).toEqualTypeOf<StorageValue | null>();

expectTypeOf(await storage.getItem<boolean>("foo")).toEqualTypeOf<
boolean | null
>();

expectTypeOf(
await storage.getItem<{ hello: string }>("foo")
).toEqualTypeOf<{ hello: string } | null>();

await storage.setItem("foo", "str");
await storage.set("bar", 1);
await storage.removeItem("foo");
Expand Down

0 comments on commit 605ec41

Please sign in to comment.