Web 3 development is a quickly expanding industry that offers developers both new problems and opportunity. It’s critical to pick a programming language that enables you to write reliable, manageable, and error-free code whether you’re developing decentralized apps (dApps) on Ethereum or other EVM-compatible blockchains. In this situation, TypeScript is useful. In this article, we’ll look about how TypeScript can make your Web 3 applications better and offer some advice on how to use TypeScript with Vite and React.
As a superset of JavaScript, TypeScript enhances the language with static typing and other capabilities, making it a popular option for developers who wish to create code that is both safer and more effective. You can discover mistakes early in the development process, write more expressive code, and increase your overall productivity by using TypeScript in Web 3 development. In this article, we’ll go over major TypeScript capabilities for Web 3 development, teach you how to set up your TypeScript setup for Vite and React development, and provide you some helpful tips for productive TypeScript development in Web 3 projects. You’ll find helpful advice and best practices to help you succeed in your Web 3 projects whether you’re new to TypeScript or an experienced developer.
There are two ways to set up a TypeScript environment with Vite for Web 3 development:
Using TypeScript as the language option during the project setup process is the simplest approach to set up a TypeScript project with Vite.
You’ll be presented with the option to select between TypeScript and JavaScript when starting a new project with Vite. By choosing TypeScript, you can immediately begin developing your Web 3 application with TypeScript and Vite thanks to the pre-configured TypeScript environment that is created, together with the appropriate plugins and customizations.
To manually set up your TypeScript environment, however, follow these instructions:
Install TypeScript
You must install TypeScript before you can use it with Vite. With the following command, you can accomplish this using npm install —save-dev typescript
Set up TypeScript in Vite
The next step is to set up Vite to work with TypeScript. To accomplish this, create a tsconfig.json file in the project’s root directory and add the required configuration values. The Vite manual provide more details on setting up TypeScript with Vite.
Installing ethers.js and useWeb3React To use ethers.js and useWeb3React in your TypeScript project, you need to install the necessary dependencies:
npm install ethers @web3-react/core @web3-react/walletconnect-connector @web3-react/injected-connector
In order to access the Web3React provider in your app, you need to wrap it in the Web3ProviderContext. Here’s an example of how to do it:
import { Web3ReactProvider } from "@web3-react/core"
import { ethers } from "ethers"
const getLibrary = (
provider: ethers.providers.ExternalProvider | ethers.providers.JsonRpcFetchFunc
) => new ethers.providers.Web3Provider(provider)
function App() {
return (
<Web3ReactProvider getLibrary={getLibrary}>
<Web3ProviderContext.Provider>
// Your app code here
</Web3ProviderContext.Provider>
</Web3ReactProvider>
)
}
Once you’ve activated the useWeb3React hook, you can access its object in all your components. Here’s an example of using the wallet connection and reading from the blockchain:
import { ethers } from "ethers"
import { Buffer } from "buffer"
import { useWeb3React } from "@web3-react/core"
import { InjectedConnector } from "@web3-react/injected-connector"
import { WalletConnectConnector } from "@web3-react/walletconnect-connector"
// Instantiate the injected connector
const injectedConnector = new InjectedConnector({
supportedChainIds: [1, 3, 4, 5, 42],
})
// Instantiate the WalletConnect connector
const walletConnectConnector = new WalletConnectConnector({
rpc: { 1: "https://mainnet.infura.io/v3/[your-project-api-key]" },
bridge: "https://bridge.walletconnect.org",
qrcode: true,
})
// Overwrite Window object
declare const window: Window & {
Buffer: any
ethereum: ethers.providers.ExternalProvider
}
window.Buffer = Buffer
function App() {
// Initialize the Web3React context
const { activate, active } = useWeb3React()
// Activate the injected connector when the component mounts
useEffect(() => {
activate(injectedConnector)
}, [activate])
// Use the Web3React context to interact with the Ethereum network
useEffect(() => {
if (active) {
const provider = new ethers.providers.Web3Provider(window.ethereum)
const signer = provider.getSigner()
// Use ethers.js to interact with the network
const getBlockNumber = async () => {
const blockNumber = await provider.getBlockNumber()
console.log(`Current block number: ${blockNumber}`)
}
getBlockNumber()
}
}, [active])
// Render the app
return (
<div className="App">
<h1>Hello, World!</h1>
</div>
)
}
Several capabilities provided by TypeScript can increase the effectiveness and dependability of your Software development in Web 3. Its static type checking, which aids in catching problems early in the development process, is one of its important characteristics. Moreover, TypeScript provides tools like interfaces and type aliases that can be used to construct complicated data structures and improve the readability and maintainability of your code. Its support for decorators, which may be utilized for tasks like dependency injection and metadata gathering, is another advantage. Finally, the support for module augmentation in TypeScript enables you to expand already-existing TypeScript definitions and include extra type information for other libraries like Ether.js.