Improve typing precision of CacheProvider.get()

This commit is contained in:
Alice Gaudon 2020-10-01 13:58:50 +02:00
parent 9d42167013
commit f41a456524
2 changed files with 5 additions and 5 deletions

View File

@ -1,5 +1,5 @@
export default interface CacheProvider {
get(key: string, defaultValue?: string): Promise<string | null>;
get<T extends string | undefined>(key: string, defaultValue?: T): Promise<T>;
has(key: string): Promise<boolean>;

View File

@ -42,8 +42,8 @@ export default class RedisComponent extends ApplicationComponent implements Cach
return this.redisClient !== undefined && this.redisClient.connected;
}
public async get(key: string, defaultValue?: string): Promise<string | null> {
return await new Promise<string | null>((resolve, reject) => {
public async get<T extends string | undefined>(key: string, defaultValue?: T): Promise<T> {
return await new Promise<T>((resolve, reject) => {
if (!this.redisClient) {
reject(`Redis store was not initialized.`);
return;
@ -54,13 +54,13 @@ export default class RedisComponent extends ApplicationComponent implements Cach
reject(err);
return;
}
resolve(val || defaultValue || null);
resolve((val || defaultValue || undefined) as T);
});
});
}
public async has(key: string): Promise<boolean> {
return await this.get(key) !== null;
return await this.get(key) !== undefined;
}
public async forget(key: string): Promise<void> {