I have this 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 }); // 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;
};
and I get these type errors:
<Parameters<typeof getCookie>[1]>
is of type OptionsType | undefined
, with type OptionsType = DefaultOptions | AppRouterOptions
.
The compiler complaint is more or less that DefaultOptions
(with has the property req?: IncomingMessage & { cookies?: TmpCookiesObj }
) is not of type AppRouterOptions
(which has the property res?: Response | NextResponse
). But since I’m deriving the types directly from the expected parameters of getCookie
, how could that be? And how could I fix that?