TypeScript 类型声明
下载 bitshares-provider.d.ts,放到项目里会被 TypeScript 包含的位置,例如 src/types/bitshares-provider.d.ts,或在 tsconfig.json 的 include / typeRoots 中引用。
文件会扩展 Window,因此在客户端代码里可以直接写:
import type { CubWalletProvider, CubAccount } from './types/bitshares-provider';
async function connect(): Promise<{ bts: CubWalletProvider; accounts: CubAccount[] }> {
const bts = window.bitshares;
if (!bts?.isCubWallet) throw new Error('请安装 CubWallet(小熊钱包)');
const accounts = await bts.request({ method: 'bts_requestAccounts' });
return { bts, accounts };
}类型里只包含已经实现的方法和事件。request 对四个方法名做了重载,返回值与源码一致。若传入其它方法名,返回类型是 Promise<unknown>,运行时会得到 不支持的方法: …。
CubWallet