TypeScript
Requirements
Wolf is designed to be as type-safe as possible! Things to keep in mind:
- Types currently require using TypeScript >=5.0.4.
- TypeScript doesn't follow semver and often introduces breaking changes in minor releases.
- Changes to types in this repository are considered non-breaking and are usually released as patch changes (otherwise every type enhancement would be a major version!).
- It is highly recommended that you lock your
starkweb/react
andtypescript
versions to specific patch releases and upgrade with the expectation that types may be fixed or upgraded between any release. - The non-type-related public API of Wagmi still follows semver very strictly.
To ensure everything works correctly, make sure your tsconfig.json
has strict
mode set to true
.
{
"compilerOptions": {
"strict": true
}
}
Config Types
By default React Context does not work well with type inference. To support strong type-safety across the React Context boundary, there are two options available:
- Declaration merging to register your
config
globally with TypeScript. config
property to pass yourconfig
directly to hooks.
Declaration Merging
Declaration merging allows you to register your config
globally with TypeScript. The Register
type enables Wolf to infer types in places that wouldn't normally have access to type info via React Context alone.
To set this up, add the following declaration to your project. Below, we co-locate the declaration merging and the config
set up.
import { createConfig, http } from "starkweb"
import { mainnet, sepolia } from "starkweb/chains"
declare module "starkweb" {
interface Register {
config: typeof config
}
}
export const config = createConfig({
chains: [mainnet, sepolia],
transports: {
[mainnet.chain_id]: http(),
[sepolia.chain_id]: http(),
},
})
Since the Register
type is global, you only need to add it once in your project. Once set up, you will get strong type-safety across your entire project. For example, query hooks will type chainId
based on your config
's chains
.
Hook config
Property
For cases where you have more than one Wagmi config
or don't want to use the declaration merging approach, you can pass a specific config
directly to hooks via the config
property.
import { createConfig, http } from "starkweb"
import { mainnet, sepolia } from "starkweb/chains"
export const configA = createConfig({
chains: [mainnet],
transports: {
[mainnet.chain_id]: http(),
},
})
export const configB = createConfig({
chains: [sepolia],
transports: {
[sepolia.chain_id]: http(),
},
})
Const-Assert ABIs & Typed Data
Wolf can infer types based on ABIs and SNIP-12 Typed Data definitions. This achieves full end-to-end type-safety from your contracts to your frontend and enlightened developer experience by autocompleting ABI item names, catching misspellings, inferring argument and return types (including overloads), and more.
For this to work, you must either const-assert ABIs and Typed Data (more info below) or define them inline. For example, useContractRead
's abi
configuration parameter:
const { data } = useContractRead({
abi: […], // <--- defined inline
})
const abi = […] as const // <--- const assertion
const { data } = useContractRead({ abi })
If type inference isn't working, it's likely you forgot to add a const
assertion or define the configuration parameter inline. Also, make sure your ABIs, Typed Data definitions, and TypeScript configuration are valid and set up correctly.
Anywhere you see the abi
or types
configuration property, you can likely use const-asserted or inline ABIs and Typed Data to get type-safety and inference.