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(types): fallback to StorageValue for un-typed stores #532

Merged
merged 3 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ export function createStorage<T extends StorageValue>(
getItem(key: string, opts = {}) {
key = normalizeKey(key);
const { relativeKey, driver } = getMount(key);
return asyncCall(driver.getItem, relativeKey, opts).then((value) =>
destr(value)
return asyncCall(driver.getItem, relativeKey, opts).then(
(value) => destr(value) as StorageValue
);
},
getItems(
Expand Down Expand Up @@ -469,7 +469,7 @@ export function createStorage<T extends StorageValue>(
remove: (key: string, opts = {}) => storage.removeItem(key, opts),
};

return storage;
return storage as unknown as Storage<T>;
}

export type Snapshot<T = string> = Record<string, T>;
Expand Down
17 changes: 11 additions & 6 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,12 @@ type StorageDefinition = {
[key: string]: unknown;
};

type StorageItemMap<T extends StorageDefinition> = T["items"];
type StorageItemMap<T> = T extends StorageDefinition ? T["items"] : T;
type StorageItemType<T, K> = K extends keyof StorageItemMap<T>
? StorageItemMap<T>[K]
: T extends StorageDefinition
? StorageValue
: T;

export interface Storage<T extends StorageValue = StorageValue> {
// Item
Expand All @@ -81,15 +86,15 @@ export interface Storage<T extends StorageValue = StorageValue> {

getItem<
U extends Extract<T, StorageDefinition>,
K extends keyof StorageItemMap<U>,
K extends string & keyof StorageItemMap<U>,
>(
key: K,
ops?: TransactionOptions
): Promise<StorageItemMap<U>[K] | null>;
getItem<U extends T>(
): Promise<StorageItemType<T, K> | null>;
getItem(
key: string,
opts?: TransactionOptions
): Promise<U | null>;
): Promise<StorageItemType<T, string> | null>;

/** @experimental */
getItems: <U extends T>(
Expand All @@ -107,7 +112,7 @@ export interface Storage<T extends StorageValue = StorageValue> {
K extends keyof StorageItemMap<U>,
>(
key: K,
value: StorageItemMap<U>[K],
value: StorageItemType<T, K>,
opts?: TransactionOptions
): Promise<void>;
setItem<U extends T>(
Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function prefixStorage<T extends StorageValue>(
if (!base) {
return storage;
}
const nsStorage: Storage = { ...storage };
const nsStorage: Storage<T> = { ...storage };
for (const property of storageKeyProperties) {
// @ts-ignore
nsStorage[property] = (key = "", ...args) =>
Expand Down
41 changes: 33 additions & 8 deletions test/storage.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,35 @@ import { createStorage } from "../src";
import type { StorageValue } from "../src";

describe("types", () => {
it("check types", async () => {
it("default types for storage", async () => {
const storage = createStorage();

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

await storage.setItem("foo", "str");
await storage.set("bar", 1);
await storage.removeItem("foo");
await storage.remove("bar");
await storage.del("baz");
});

it("indexed types for storage", async () => {
const storage = createStorage<string>();

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

await storage.setItem("foo", "str");
// @ts-expect-error should be a string
await storage.set("bar", 1);

await storage.removeItem("foo");
await storage.remove("bar");
await storage.del("baz");
});

it("namespaced types for storage", async () => {
type TestObjType = {
a: number;
b: boolean;
Expand All @@ -17,15 +45,12 @@ describe("types", () => {
};
const storage = createStorage<MyStorage>();

expectTypeOf(await storage.getItem("foo")).toMatchTypeOf<string | null>();
expectTypeOf(await storage.getItem("bar")).toMatchTypeOf<number | null>();
expectTypeOf(await storage.getItem("foo")).toEqualTypeOf<string | null>();
expectTypeOf(await storage.getItem("bar")).toEqualTypeOf<number | null>();
expectTypeOf(
await storage.getItem("unknown")
).toMatchTypeOf<StorageValue | null>();
expectTypeOf(await storage.get("baz")).toMatchTypeOf<TestObjType | null>();
expectTypeOf(
await storage.getItem("aaaaa")
).toMatchTypeOf<MyStorage | null>();
).toEqualTypeOf<StorageValue | null>();
expectTypeOf(await storage.get("baz")).toEqualTypeOf<TestObjType | null>();

// @ts-expect-error
await storage.setItem("foo", 1); // ts err: Argument of type 'number' is not assignable to parameter of type 'string'
Expand Down
Loading