Skip to content

Commit

Permalink
fix: typings of fastify.cache.get
Browse files Browse the repository at this point in the history
  • Loading branch information
Uzlopak committed Dec 7, 2023
1 parent 3733c8c commit 4281478
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
7 changes: 5 additions & 2 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,13 @@ declare namespace fastifyCaching {
* @link [`abstract-cache` protocol documentation](https://github.com/jsumners/abstract-cache#protocol)
*/
export interface AbstractCacheCompliantObject {
get(
get<T = unknown>(
key: string | { id: string; segment: string },
callback?: (error: unknown, result: unknown) => void
callback: (error: unknown, result: (T | undefined)) => void
): void;
get<T = unknown>(
key: string | { id: string; segment: string },
): (T | undefined);
set(
key: string | { id: string; segment: string },
value: unknown,
Expand Down
17 changes: 17 additions & 0 deletions types/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,20 @@ const badCachingOptions = {
};

expectError(shouldErrorApp.register(fastifyCaching, badCachingOptions));

fastify.get('/three', async (request, reply) => {
expectType<unknown>(
fastify.cache.get('well-known')
);
expectType<string|undefined>(
fastify.cache.get<string>('well-known')
);
expectType<void>(
fastify.cache.get<string>('well-known', (err, value) => {
expectType<unknown>(err);
expectType<string|undefined>(value);
})
);

return { message: 'two' };
});

0 comments on commit 4281478

Please sign in to comment.