diff --git a/packages/wxt/src/__tests__/storage.test.ts b/packages/wxt/src/__tests__/storage.test.ts index 6d3e838d1..d5fae8522 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 1450912ec..55795d4a7 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.