[Ethereum] Trying to use window.ethereum.request in Typescript errors out with Property ‘request’ does not exist on type ‘EthereumProvider’

metamasktypescript

Except for one unanswered question from last November, I cannot find anything . . . .

I am revamping some typescript code (in Visual Studio) for the Metamask January change.
Previously, I was using web3. Now I am just trying to use window.ethereum.

I cannot find a DefinitelyTyped (or any other types) file with type declarations.

I fear that this is really something simple/stupid but I'm totally at a loss.

Best Answer

If you're doing this in React with CRA or ViteJS.

File: src/vite-env.d.ts

/// <reference types="vite/client" />

interface Window {
  ethereum: any;
}

File: src/react-app-env.d.ts

/// <reference types="react-scripts" />

interface Window {
  ethereum: any;
}