Within my Angular service, there is a method that retrieves data from Sync Storage:
getFromSyncStorage(key: string): Promise<Object | LastErrorType> {
return new Promise(function (resolve, reject) {
chrome.storage.sync.get(key, function (v: Object) {
if (chrome.runtime.lastError) {
return reject(chrome.runtime.lastError);
}
resolve(v && v[key]);
});
});
}
It's important to note that LastErrorType
is defined as:
export type LastErrorType = typeof chrome.runtime.lastError;
The challenge arises when predicting the return type based on the key passed. For instance:
getAllRunHistory() {
return this.cds.getFromSyncStorage('my-special-key');
}
Attempting to specify the return type as an array results in a TypeScript error:
getAllRunHistory() : Promise<Array<any>>{
return this.cds.getFromSyncStorage('my-special-key');
}
How can generics be utilized to properly type this versatile getFromSyncStorage
method?