Skip to content

TypeScript

Requirements

Khizab Core is designed to be as type-safe as possible! Things to keep in mind:

  • Types currently require using TypeScript >=5.0.4.
  • 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 @khizab/core and typescript versions to specific patch releases and upgrade with the expectation that types may be fixed or upgraded between any release.

To ensure everything works correctly, make sure your tsconfig.json has strict mode set to true.

json
{
  "compilerOptions": {
    "strict": true
  }
}
{
  "compilerOptions": {
    "strict": true
  }
}

Const-Assert ABIs & Typed Data

Khizab Core can infer types based on ABIs. 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. For example, useReadContract's abi configuration parameter:

ts
const abi = [] as const // <--- const assertion 
const result = readContract({ abi })
const abi = [] as const // <--- const assertion 
const result = readContract({ abi })

If type inference isn't working, it's likely you forgot to add a const assertion. Also, make sure your ABIs, and TypeScript configuration are valid and set up correctly.

TIP

Unfortunately TypeScript doesn't support importing JSON as const yet. Check out the Khizab CLI to help with this! It can automatically fetch ABIs from Aptos blochain.

Anywhere you see the abi configuration property, you can likely use const-asserted to get type-safety and inference. These properties are also called out in the docs.

Here's what readContract looks like with a const-asserted abi property.

ts
ts
import { readContract } from '@khizab/core'
 
const result = await readContract(config, {
abi,
functionName: 'get_todo_list_counter',
(property) functionName: "get_todo_list_counter"
args: ['0x000000'],
(property) args: [string]
})
result
const result: readonly [bigint]
ts
import { readContract } from '@khizab/core'
 
const result = await readContract(config, {
abi,
functionName: 'get_todo_list_counter',
(property) functionName: "get_todo_list_counter"
args: ['0x000000'],
(property) args: [string]
})
result
const result: readonly [bigint]

Released under the MIT License.