2

I'm trying to use ethers in a Next/Typescript project:

code:

if (typeof window.ethereum !== 'undefined') {}

Error:

Property 'ethereum' does not exist on type 'Window & typeof globalThis'

I have tried to extend the Window interface in my next-env.d.ts file but the error persists.

next-env.d.ts:

/// <reference types="next" />
/// <reference types="next/image-types/global" />

// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.
declare global {
  interface Window {
    ethereum: any
  }
}

Is there a specific fix for Next.js?

1 Answer 1

2

The next file should not be edited - you can create another declaration file like globals.d.ts with this content:

declare global {
  interface Window {
    ethereum: any
  }
}

and it should work

1
  • Somehow that does not work. I declared it inside the file that gives me TS warning
    – Russo
    Oct 18, 2023 at 2:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.