From f44bdb8caa5a2e7785989377ec61d0a68de2696d Mon Sep 17 00:00:00 2001 From: Dan McGee Date: Wed, 24 Sep 2025 08:49:22 -0500 Subject: [PATCH] fix: Add another `defineItem` signature when `init` function is passed https://github.com/wxt-dev/wxt/pull/1601 adjusted the typing of `defineItem` to examine whether `defaultValue` or `fallback` were passed, and only then, remove the `null` from the possible returned value. However, it did not accommodate the case where an `init` function is passed to prevent a `null` return, which was previously allowed. Add a new type signature for this case, and add some tests for type expectations to catch the issue. --- packages/storage/src/__tests__/index.test.ts | 12 ++++++++++++ packages/storage/src/index.ts | 6 ++++++ 2 files changed, 18 insertions(+) diff --git a/packages/storage/src/__tests__/index.test.ts b/packages/storage/src/__tests__/index.test.ts index 200de6aa2..1ba64e3cd 100644 --- a/packages/storage/src/__tests__/index.test.ts +++ b/packages/storage/src/__tests__/index.test.ts @@ -1315,6 +1315,18 @@ describe('Storage Utils', () => { }); expectTypeOf(item).toEqualTypeOf>(); }); + + it('should define a non-null value when options are passed with a non-null init function', () => { + const item = storage.defineItem(`local:test`, { + init: () => 123, + }); + expectTypeOf(item).toEqualTypeOf>(); + + const item2 = storage.defineItem(`local:test`, { + init: () => Promise.resolve(123), + }); + expectTypeOf(item2).toEqualTypeOf>(); + }); }); }); diff --git a/packages/storage/src/index.ts b/packages/storage/src/index.ts index 76e438251..790d4337f 100644 --- a/packages/storage/src/index.ts +++ b/packages/storage/src/index.ts @@ -749,6 +749,12 @@ export interface WxtStorage { key: StorageItemKey, options: WxtStorageItemOptions & { defaultValue: TValue }, ): WxtStorageItem; + defineItem = {}>( + key: StorageItemKey, + options: WxtStorageItemOptions & { + init: () => TValue | Promise; + }, + ): WxtStorageItem; defineItem = {}>( key: StorageItemKey, options: WxtStorageItemOptions,