Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

142
Vistas
How to write typescript interface for Window object ethereum that has a request method being called

I am running the following function

import { ethers } from "ethers";


async function requestAccount() {
    await window.ethereum.request({ method: "eth_requestAccounts" });
  }

The issue I am having is I am using typescript and it complains with the following error

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

So I was able to fix it with the following

declare global {
  interface Window{
    ethereum?:any
  }
}

However I don't think this is taking advantage of typescript properly. How would I write it so that the interface value is correct. I assume it should be an object with a method inside, but not sure how to write this in typescript.

Any help would be greatly appreciated.

Thanks

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

This is a quite old question but if you or someone else is still looking for an answer, you can use the ExternalProvider type of ethers.

interface Window {
  ethereum?: import('ethers').providers.ExternalProvider;
}

Here is the extracted version of ExternalProvider if you're not using ethers

type ExternalProvider = {
  isMetaMask?: boolean;
  isStatus?: boolean;
  host?: string;
  path?: string;
  sendAsync?: (request: { method: string, params?: Array<any> }, callback: (error: any, response: any) => void) => void
  send?: (request: { method: string, params?: Array<any> }, callback: (error: any, response: any) => void) => void
  request?: (request: { method: string, params?: Array<any> }) => Promise<any>
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

I resolved the Type Error temporarily by:

  • add: declare let window: any at the top level after your imports. Now your are able to call window.ethereum with the warning and Type Error off.

I think this overrides the global window object with a type any I'm sure this is not the best way to fix this, sort of a hack but fixes the Type error in the meantime. Let me know if this worked for your case.

about 4 years ago · Juan Pablo Isaza Denunciar

0

npm i @metamask/providers

Install the above package:

import { MetaMaskInpageProvider } from "@metamask/providers";

declare global {
  interface Window{
    ethereum?:MetaMaskInpageProvider
  }
}

then

const accounts = (await ethereum?.request({
      method: "eth_requestAccounts",
    })) as string[];
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda