And*_*son 1 javascript typescript ethereum metamask ethers.js
我正在运行以下函数
import { ethers } from "ethers";
async function requestAccount() {
await window.ethereum.request({ method: "eth_requestAccounts" });
}
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是我正在使用打字稿,它抱怨以下错误
Property 'ethereum' does not exist on type 'Window & typeof globalThis'
所以我能够用以下方法修复它
declare global {
interface Window{
ethereum?:any
}
}
Run Code Online (Sandbox Code Playgroud)
但是我认为这没有正确利用打字稿。我该如何编写才能使接口值正确。我认为它应该是一个内部有方法的对象,但不确定如何在打字稿中编写它。
任何帮助将不胜感激。
谢谢
这是一个相当老的问题,但如果您或其他人仍在寻找答案,您可以使用ExternalProvider
的类型ethers
。
interface Window {
ethereum?: import('ethers').providers.ExternalProvider;
}
Run Code Online (Sandbox Code Playgroud)
ExternalProvider
这是如果您不使用以太币的提取版本
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>
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2635 次 |
最近记录: |