Skip to content

Commit 2ba4dec

Browse files
authored
Add a generic type for unstable_cache (#49166)
## For Contributors I know it's an unstable api but this just makes it easier to test with. This uses the type of the callback to `unstable_cache` as the return type of `unstable_cache`.
1 parent bc5164f commit 2ba4dec

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

packages/next/src/server/web/spec-extension/unstable-cache.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@ import {
55

66
type Callback = (...args: any[]) => Promise<any>
77

8-
// TODO: generic callback type?
9-
export function unstable_cache(
10-
cb: Callback,
8+
export function unstable_cache<T extends Callback>(
9+
cb: T,
1110
keyParts: string[],
1211
options: {
1312
revalidate: number | false
1413
tags?: string[]
1514
}
16-
): Callback {
15+
): T {
1716
const joinedKey = cb.toString() + '-' + keyParts.join(', ')
1817
const staticGenerationAsyncStorage = (
1918
fetch as any
@@ -34,7 +33,7 @@ export function unstable_cache(
3433
)
3534
}
3635

37-
return async (...args: any[]) => {
36+
const cachedCb = async (...args: any[]) => {
3837
// We override the default fetch cache handling inside of the
3938
// cache callback so that we only cache the specific values returned
4039
// from the callback instead of also caching any fetches done inside
@@ -131,4 +130,6 @@ export function unstable_cache(
131130
}
132131
)
133132
}
133+
// TODO: once AsyncLocalStorage.run() returns the correct types this override will no longer be necessary
134+
return cachedCb as unknown as T
134135
}

0 commit comments

Comments
 (0)