From 95163243d3b768670cb84a110804d68cd6ac8669 Mon Sep 17 00:00:00 2001 From: Baraich Date: Sat, 28 Sep 2024 23:50:10 +0530 Subject: [PATCH 1/6] fix: #1005 fixed, removed Promise and made type more concise. --- packages/wxt/src/storage.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/wxt/src/storage.ts b/packages/wxt/src/storage.ts index 1450912ec..5b9cac0a9 100644 --- a/packages/wxt/src/storage.ts +++ b/packages/wxt/src/storage.ts @@ -461,7 +461,10 @@ export interface WxtStorage { * @example * await storage.getItem("local:installDate"); */ - getItem(key: StorageItemKey, opts?: GetItemOptions): Promise; + getItem = GetItemOptions>( + key: StorageItemKey, + opts?: O, + ): Promise; /** * Get multiple items from storage. The return order is guaranteed to be the same as the order * requested. From dedd5664645f3c19613a6688fd32e62a63b28e08 Mon Sep 17 00:00:00 2001 From: Aaron Date: Mon, 30 Sep 2024 09:06:30 -0500 Subject: [PATCH 2/6] Add storage tests --- packages/wxt/src/__tests__/storage.test.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/packages/wxt/src/__tests__/storage.test.ts b/packages/wxt/src/__tests__/storage.test.ts index 6d3e838d1..675e3ecb2 100644 --- a/packages/wxt/src/__tests__/storage.test.ts +++ b/packages/wxt/src/__tests__/storage.test.ts @@ -903,5 +903,17 @@ describe('Storage Utils', () => { // @ts-expect-error await storage.getItem('loca:test').catch(() => {}); }); + + it('should return a nullable type when getItem is called without a fallback', async () => { + const res = await storage.getItem('local:test'); + expectTypeOf(res).toBeNullable(); + }); + + it('should return a nullable type when getItem is called without a fallback', async () => { + const res = await storage.getItem('local:test', { + fallback: 'test', + }); + expectTypeOf(res).not.toBeNullable(); + }); }); }); From cc3950585053af3072f55606253ac470162298bb Mon Sep 17 00:00:00 2001 From: Aaron Date: Mon, 30 Sep 2024 09:09:04 -0500 Subject: [PATCH 3/6] More descriptive type param names --- packages/wxt/src/storage.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/packages/wxt/src/storage.ts b/packages/wxt/src/storage.ts index 5b9cac0a9..7649b13b0 100644 --- a/packages/wxt/src/storage.ts +++ b/packages/wxt/src/storage.ts @@ -461,10 +461,15 @@ export interface WxtStorage { * @example * await storage.getItem("local:installDate"); */ - getItem = GetItemOptions>( + getItem< + TValue, + TOptions extends GetItemOptions = GetItemOptions, + >( key: StorageItemKey, - opts?: O, - ): Promise; + opts?: TOptions, + ): Promise< + TOptions extends { fallback: infer TFallback } ? TFallback : TValue | null + >; /** * Get multiple items from storage. The return order is guaranteed to be the same as the order * requested. From bb868716b2bbec1e72e19d6b7f6125c7b58095ed Mon Sep 17 00:00:00 2001 From: Aaron Date: Mon, 30 Sep 2024 09:11:07 -0500 Subject: [PATCH 4/6] Fix test name --- packages/wxt/src/__tests__/storage.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/wxt/src/__tests__/storage.test.ts b/packages/wxt/src/__tests__/storage.test.ts index 675e3ecb2..a32db8294 100644 --- a/packages/wxt/src/__tests__/storage.test.ts +++ b/packages/wxt/src/__tests__/storage.test.ts @@ -909,7 +909,7 @@ describe('Storage Utils', () => { expectTypeOf(res).toBeNullable(); }); - it('should return a nullable type when getItem is called without a fallback', async () => { + it('should return a non-null type when getItem is called with a fallback', async () => { const res = await storage.getItem('local:test', { fallback: 'test', }); From 3b89c765f4e53175075097b2ab248a84d4de6e75 Mon Sep 17 00:00:00 2001 From: Aaron Date: Mon, 30 Sep 2024 09:16:24 -0500 Subject: [PATCH 5/6] Add second test --- packages/wxt/src/__tests__/storage.test.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/packages/wxt/src/__tests__/storage.test.ts b/packages/wxt/src/__tests__/storage.test.ts index a32db8294..d5fae8522 100644 --- a/packages/wxt/src/__tests__/storage.test.ts +++ b/packages/wxt/src/__tests__/storage.test.ts @@ -910,6 +910,13 @@ describe('Storage Utils', () => { }); it('should return a non-null type when getItem is called with a fallback', async () => { + const res = await storage.getItem('local:test', { + fallback: 'test', + }); + expectTypeOf(res).not.toBeNullable(); + }); + + it('should return a non-null type when getItem is called with a fallback and the first type parameter is passed', async () => { const res = await storage.getItem('local:test', { fallback: 'test', }); From 0d44ed7acb0b6e96e56ff722aa9cde6a4ae21f0e Mon Sep 17 00:00:00 2001 From: Baraich Date: Mon, 30 Sep 2024 20:51:45 +0530 Subject: [PATCH 6/6] bug fixed, added overload function --- packages/wxt/src/storage.ts | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/packages/wxt/src/storage.ts b/packages/wxt/src/storage.ts index 7649b13b0..55795d4a7 100644 --- a/packages/wxt/src/storage.ts +++ b/packages/wxt/src/storage.ts @@ -461,15 +461,16 @@ export interface WxtStorage { * @example * await storage.getItem("local:installDate"); */ - getItem< - TValue, - TOptions extends GetItemOptions = GetItemOptions, - >( + getItem( key: StorageItemKey, - opts?: TOptions, - ): Promise< - TOptions extends { fallback: infer TFallback } ? TFallback : TValue | null - >; + opts: GetItemOptions & { fallback: TValue }, + ): Promise; + + getItem( + key: StorageItemKey, + opts?: GetItemOptions, + ): Promise; + /** * Get multiple items from storage. The return order is guaranteed to be the same as the order * requested.