Skip to content

Commit 5d0a266

Browse files
authored
fix: Add another defineItem signature when init function is passed (#1909)
1 parent 2466786 commit 5d0a266

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

packages/storage/src/__tests__/index.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1315,6 +1315,18 @@ describe('Storage Utils', () => {
13151315
});
13161316
expectTypeOf(item).toEqualTypeOf<WxtStorageItem<number | null, {}>>();
13171317
});
1318+
1319+
it('should define a non-null value when options are passed with a non-null init function', () => {
1320+
const item = storage.defineItem(`local:test`, {
1321+
init: () => 123,
1322+
});
1323+
expectTypeOf(item).toEqualTypeOf<WxtStorageItem<number, {}>>();
1324+
1325+
const item2 = storage.defineItem(`local:test`, {
1326+
init: () => Promise.resolve(123),
1327+
});
1328+
expectTypeOf(item2).toEqualTypeOf<WxtStorageItem<number, {}>>();
1329+
});
13181330
});
13191331
});
13201332

packages/storage/src/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,12 @@ export interface WxtStorage {
752752
key: StorageItemKey,
753753
options: WxtStorageItemOptions<TValue> & { defaultValue: TValue },
754754
): WxtStorageItem<TValue, TMetadata>;
755+
defineItem<TValue, TMetadata extends Record<string, unknown> = {}>(
756+
key: StorageItemKey,
757+
options: WxtStorageItemOptions<TValue> & {
758+
init: () => TValue | Promise<TValue>;
759+
},
760+
): WxtStorageItem<TValue, TMetadata>;
755761
defineItem<TValue, TMetadata extends Record<string, unknown> = {}>(
756762
key: StorageItemKey,
757763
options: WxtStorageItemOptions<TValue>,

0 commit comments

Comments
 (0)