Skip to content

Commit

Permalink
docs: window.ethereum section in typescript.md (#993)
Browse files Browse the repository at this point in the history
* docs: add Ethereum Provider section

* Update typescript.md

* Update typescript.md

---------

Co-authored-by: jxom <jakemoxey@gmail.com>
  • Loading branch information
KipCrossing and jxom authored Aug 8, 2023
1 parent 4bdbf15 commit 8031562
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions site/docs/typescript.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,21 @@ The following utilities support type inference when you use const assertions or
## Configuring Internal Types

For advanced use-cases, you may want to configure viem's internal types. Most of viem's types relating to ABIs and EIP-712 Typed Data are powered by [ABIType](https://abitype.dev). See ABIType's [documentation](https://abitype.dev/config.html) for more info on how to configure types.

## `window` Polyfill

By importing the `viem/window` Polyfill, the global `window.ethereum` will typed as an [`EIP1193Provider`](https://github.com/wagmi-dev/viem/blob/4bdbf15be0d61b52a195e11c97201e707fb616cc/src/types/eip1193.ts#L24-L26) (including a fully-typed `request` function & typed events). It may be `undefined` in cases where no browser extension Wallet is detected, or if rendered on the server.

```ts
import 'viem/window';

if (typeof window !== 'undefined') {
const transaction = await window.ethereum.request({
// ^? const transaction: Transaction
method: 'eth_getTransactionByHash',
// ^? (property) method: "eth_blockNumber" | "eth_call" | ...
params: ['0x...']
// ^? (property) params: [hash: Hash]
})
}
```

1 comment on commit 8031562

@vercel
Copy link

@vercel vercel bot commented on 8031562 Aug 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.