From 5faa5d7537610dc0b8a1432ae1d0239703531c2d Mon Sep 17 00:00:00 2001 From: Gurvir Singh <146362414+baraich@users.noreply.github.com> Date: Wed, 2 Oct 2024 18:50:32 +0530 Subject: [PATCH] fix: #1005 fixed, by updating type-definations to getItem method. (#1007) Co-authored-by: Aaron --- packages/wxt/src/__tests__/storage.test.ts | 19 +++++++++++++++++++ packages/wxt/src/storage.ts | 11 ++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/packages/wxt/src/__tests__/storage.test.ts b/packages/wxt/src/__tests__/storage.test.ts index 6d3e838d..d5fae852 100644 --- a/packages/wxt/src/__tests__/storage.test.ts +++ b/packages/wxt/src/__tests__/storage.test.ts @@ -903,5 +903,24 @@ 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 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', + }); + expectTypeOf(res).not.toBeNullable(); + }); }); }); diff --git a/packages/wxt/src/storage.ts b/packages/wxt/src/storage.ts index 1450912e..55795d4a 100644 --- a/packages/wxt/src/storage.ts +++ b/packages/wxt/src/storage.ts @@ -461,7 +461,16 @@ export interface WxtStorage { * @example * await storage.getItem("local:installDate"); */ - getItem(key: StorageItemKey, opts?: GetItemOptions): Promise; + getItem( + key: StorageItemKey, + 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.