nextauthjs / next-auth

Authentication for the Web.
https://authjs.dev
ISC License
24.96k stars 3.52k forks source link

TypeScript Error: 'typeof globalThis' has no index signature #8358

Closed Arjun259194 closed 7 months ago

Arjun259194 commented 1 year ago

What is the improvement or update you wish to see?

Adding these steps to docs for Typescript projects.

  1. Create a new TypeScript declaration file (e.g., types/mongodb.d.ts).
  2. In the declaration file, import the necessary types from MongoDB and declare a global variable for _mongoClientPromise.
// types/mongodb.d.ts
import { MongoClient } from 'mongodb'

declare global {
  var _mongoClientPromise: Promise<MongoClient>
}

Is there any context that might help us understand?

I encountered a TypeScript error in my Next.js project where I was using MongoDB. The error message was "Element implicitly has an 'any' type because type 'typeof globalThis' has no index signature." After some investigation, I found that this issue can be resolved by adding a global variable declaration in a custom TypeScript declaration file.

By following this solution, you provide TypeScript with the necessary type information for the global variable, resolving the type-checking issue.

Does the docs page already exist? Please link to it.

https://authjs.dev/reference/adapter/mongodb

ndom91 commented 7 months ago

Thanks for sharing!