fix: #1005 fixed, by updating type-definations to getItem method. (#1007)

Co-authored-by: Aaron <aaronklinker1@gmail.com>
This commit is contained in:
Gurvir Singh
2024-10-02 18:50:32 +05:30
committed by GitHub
parent 177223c625
commit 5faa5d7537
2 changed files with 29 additions and 1 deletions
@@ -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();
});
});
});
+10 -1
View File
@@ -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.