In my src/react-app-env.d.ts I am using
/// <reference types="react-scripts" />
import { ExternalProvider } from "@ethersproject/providers";
declare global {
interface Window {
ethereum?: ExternalProvider;
}
}
Notice that @ethersproject/providers
is an ethers
dependency so you do not need to install it.
Then I also added a src/hooks/useMetaMask.ts file with useMetaMask
hook that casts the provider to MetaMask provider type. This can be useful if you need to add MetaMask listeners.
import type { MetaMaskInpageProvider } from "@metamask/providers";
export const useMetaMask = () => {
const ethereum = global?.window?.ethereum;
if (!ethereum || !ethereum.isMetaMask) return;
return ethereum as unknown as MetaMaskInpageProvider;
};
MetaMaskInpageProvider
would make it only compatible for MetaMask? – Masculine