Skip to content

Commit

Permalink
fix: #1005 fixed, by updating type-definations to getItem method. (#1007
Browse files Browse the repository at this point in the history
)

Co-authored-by: Aaron <[email protected]>
  • Loading branch information
baraich and aklinker1 authored Oct 2, 2024
1 parent 177223c commit 5faa5d7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
19 changes: 19 additions & 0 deletions packages/wxt/src/__tests__/storage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>('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<string>('local:test', {
fallback: 'test',
});
expectTypeOf(res).not.toBeNullable();
});
});
});
11 changes: 10 additions & 1 deletion packages/wxt/src/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,16 @@ export interface WxtStorage {
* @example
* await storage.getItem<number>("local:installDate");
*/
getItem<T>(key: StorageItemKey, opts?: GetItemOptions<T>): Promise<T | null>;
getItem<TValue>(
key: StorageItemKey,
opts: GetItemOptions<TValue> & { fallback: TValue },
): Promise<TValue>;

getItem<TValue>(
key: StorageItemKey,
opts?: GetItemOptions<TValue>,
): Promise<TValue | null>;

/**
* Get multiple items from storage. The return order is guaranteed to be the same as the order
* requested.
Expand Down

0 comments on commit 5faa5d7

Please sign in to comment.