개발자가 된 감자

Property 'accessToken' does not exist on type 'User | AdapterUser'. Property 'accessToken' does not exist on type 'User'.ts(2339) 본문

Error 해결

Property 'accessToken' does not exist on type 'User | AdapterUser'. Property 'accessToken' does not exist on type 'User'.ts(2339)

감자씨 2025. 2. 19. 15:11
728x90

next-auth 를 사용한 로그인 토큰 설정 중 해당 오류가 발생했다.

이 오류는 TypeScript에서 User 또는 AdapterUser 타입에 accessToken 속성이 정의되어 있지 않기 때문에 발생했다.

이를 해결하려면 User 타입에 accessToken 속성을 추가하여 타입 선언을 확장해야 한다.

 

1. types/next-auth.d.ts 파일 생성

프로젝트 루트 디렉토리에 types 폴더를 만들거나, 본인이 기존에 사용하고 있는 types 폴더 하위에

types/next-auth.d.ts 파일을 생성한다. 이 파일에서 User 타입을 확장시킨다.

import NextAuth from "next-auth";

declare module "next-auth" {
  interface User {
    id: string;
    name: string;
    accessToken: string;
  }

  interface Session {
    user: User;
  }
};

 

2. tsconfig.json 파일 업데이트

TypeScript가 새로운 타입 선언 파일을 인식할 수 있도록 tsconfig.json 파일을 변경해준다.

...
"include": [
    "next-env.d.ts",
    ...
],
...

 

 

저장 후 해당 오류가 사라진 것을 확인할 수 있다!

 

728x90
Comments