question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to augment Session using TypeScript?

See original GitHub issue

Your question

Hi, would it be possible to add an example of type augmentation to the documentation?

What are you trying to do

I’ve modified what the session object returns and would like to augment the type defined in next-auth and next-auth/client to reflect the changes I’ve made. I’ve tried overriding the type definition by creating a next-auth.d.ts at the root of the project and included it in my tsconfig.json. However, VSCode uses the one defined by the module and not the one I’ve created (even though it recognizes that there are two type definitions).

To show what I have, here’s the type definition I’ve created:

// next-auth.d.ts

import 'next-auth' 
import NextAuth from 'next-auth' 
import * as auth from 'next-auth' 
// import * as client from 'next-auth/client' 
// import 'next-auth/client' 

declare module 'next-auth' {
  export * from 'next-auth'
  export type InitOptions = auth.InitOptions
  export default NextAuth
  interface Session {
    user: {
      name: string
      email: string
      image: string
      uid: string
    }
  }
}

declare module 'next-auth/client' {
  export * from 'next-auth/client'

  export interface Session {
    user: {
      name: string
      email: string
      image: string
      uid: string
    }
  }
}


Any help would be appreciated.

Thank you.

Feedback Documentation refers to searching through online documentation, code comments and issue history. The example project refers to next-auth-example.

  • Found the documentation helpful
  • Found documentation but was incomplete
  • Could not find relevant documentation
  • Found the example project helpful
  • Did not find the example project helpful

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:29 (7 by maintainers)

github_iconTop GitHub Comments

18reactions
kripodcommented, Oct 15, 2020

I’ve just recently made the User model augmentable with @types/next-auth, by adding an auth.d.ts file with the contents below:

import "next-auth";

declare module "next-auth" {
  interface User {
    id: number; // Or string
  }
}
17reactions
georgeromancommented, Apr 30, 2021

@sualko Try the following:

import "next-auth";

declare module "next-auth" {
  interface User {
    id: number;
  }

  interface Session {
    user: User;
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeScript - NextAuth.js
Good examples are Session and JWT . Ideally, you should only need to create these types at a single place, and TS should...
Read more >
how to update the type of session in session callback in Next ...
I am using typescript and my [..
Read more >
How to Extend User and Session in NextAuth.js (Typescript)
In this tutorial, you'll learn how to extend the User and Session object in nextauth.js using Typescript's declaration merging and module ...
Read more >
Documentation - Declaration Merging - TypeScript
In TypeScript, a declaration creates entities in at least one of three groups: namespace, ... You can use module augmentation to tell the...
Read more >
How to use express-session with your custom SessionData ...
Turns out all you have to do is to add this declaration merging to one of your top-level TypeScript files. I tried adding...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found