iron-session icon indicating copy to clipboard operation
iron-session copied to clipboard

Argument of type 'ReadonlyRequestCookies' is not assignable to parameter of type 'CookieStore'

Open Barzi-Ahmed opened this issue 5 months ago • 6 comments

Hello,

Next.js version: 15.0.0-canary.134 iron-session version: 8.0.3

cookies() in the getIronSession parameter, has the following TS error:

Argument of type 'ReadonlyRequestCookies' is not assignable to parameter of type 'CookieStore'.
  Types of property 'set' are incompatible.
    Type '(...args: [key: string, value: string, cookie?: Partial<ResponseCookie>] | [options: ResponseCookie]) => ResponseCookies' is not assignable to type '{ (name: string, value: string, cookie?: Partial<ResponseCookie> | undefined): void; (options: ResponseCookie): void; }'.
      Types of parameters 'args' and 'name' are incompatible.
        Type '[name: string, value: string, cookie?: Partial<ResponseCookie> | undefined]' is not assignable to type '[key: string, value: string, cookie?: Partial<ResponseCookie>] | [options: ResponseCookie]'.
          Type '[name: string, value: string, cookie?: Partial<ResponseCookie> | undefined]' is not assignable to type '[key: string, value: string, cookie?: Partial<ResponseCookie>]'.
            Type at position 2 in source is not compatible with type at position 2 in target.
              Type 'Partial<ResponseCookie> | undefined' is not assignable to type 'Partial<ResponseCookie>'.
                Type 'undefined' is not assignable to type 'Partial<ResponseCookie>'.ts(2345)
(alias) cookies(): ReadonlyRequestCookies
import cookies

This is my code:

'use server'

import { type SessionOptions, getIronSession } from 'iron-session'
import { cookies } from 'next/headers'
import { SESSION_SECRET_KEY } from '@/constants'

export interface SessionData {
    username: string
    isLoggedIn: boolean
    counter: number
}

const sessionOptions: SessionOptions = {
    password: SESSION_SECRET_KEY,
    cookieName: 'session',
    cookieOptions: {
        secure: process.env.NODE_ENV === 'production',
    },
}

export const getServerActionSession = async () => {
    const session = await getIronSession<SessionData>(cookies(), sessionOptions)
    return session
}

export default getServerActionSession

Barzi-Ahmed avatar Sep 02 '24 12:09 Barzi-Ahmed