I have an issue with the following method:
import { getCookie } from 'cookies-next';
export const getAccessTokenFromCookies = (
req?: NonNullable<Parameters<typeof getCookie>[1]>['req'],
res?: NonNullable<Parameters<typeof getCookie>[1]>['res'],
): string | undefined => {
const session = getCookie(sessionKey, { req, res }); // encountering type errors for req and res
if (session) {
const parsedSession = JSON.parse(session);
return typeof parsedSession === 'object' && parsedSession != null ? parsedSession[accessTokenKey] : undefined;
} else return undefined;
};
However, I am facing certain type errors as pointed out in the screenshot here: https://i.sstatic.net/bZyjpXmU.png
Upon inspecting the types of getCookie
here, I found that
<Parameters<typeof getCookie>[1]>
has a type of `OptionsType | undefined`, where OptionsType = DefaultOptions | AppRouterOptions
.
The compiler error seems to indicate that DefaultOptions
(which includes the property req?: IncomingMessage & { cookies?: TmpCookiesObj }
) is not compatible with AppRouterOptions
(which includes the property res?: Response | NextResponse
).
Considering that I'm deriving the types directly from the expected parameters of getCookie
, I'm puzzled about this discrepancy. What measures can be taken to address this issue?
Edit: I found that declaring
options?: NonNullable<Parameters<typeof getCookie>[1]>
and passing it to getCookies
resolves the issue.