TypeScript bindings generator for Ethereum smart contracts that transforms ABI files into type-safe interfaces and classes
npx @tessl/cli install tessl/npm-typechain@8.3.0TypeChain is a TypeScript bindings generator for Ethereum smart contracts. It transforms ABI files into type-safe TypeScript interfaces, classes, and types, providing compile-time type checking and IntelliSense support for smart contract interactions.
npm install typechainimport { runTypeChain, Config, PublicConfig, TypeChainTarget } from "typechain";For CommonJS:
const { runTypeChain } = require("typechain");import { runTypeChain } from "typechain";
// Generate TypeScript bindings from ABI files
const config = {
cwd: process.cwd(),
filesToProcess: ["./artifacts/**/*.json"],
allFiles: ["./artifacts/**/*.json"],
target: "ethers-v5",
outDir: "./typechain-types"
};
const result = await runTypeChain(config);
console.log(`Generated ${result.filesGenerated} files`);TypeChain is built around several key components:
runTypeChain function orchestrates the entire generation processMain entry point for programmatic usage of TypeChain with configuration management and target resolution.
function runTypeChain(config: PublicConfig): Promise<{ filesGenerated: number }>;
interface PublicConfig {
cwd: string;
target: string;
outDir?: string;
prettier?: object;
filesToProcess: string[];
allFiles: string[];
inputDir?: string;
flags?: CodegenConfig;
}Complete ABI parsing system that converts raw JSON ABI files into structured TypeScript representations with full type safety.
function parse(abi: RawAbiDefinition[], path: string, documentation?: DocumentationResult): Contract;
function extractAbi(rawJson: string): RawAbiDefinition[];
function extractBytecode(rawContents: string): BytecodeWithLinkReferences | undefined;
interface Contract {
name: string;
rawName: string;
path: string[];
fallback?: FunctionWithoutInputDeclaration;
constructor: FunctionWithoutOutputDeclaration[];
functions: Dictionary<FunctionDeclaration[]>;
events: Dictionary<EventDeclaration[]>;
structs: Dictionary<StructType[]>;
documentation?: object;
}Tools for generating TypeScript code including barrel files, imports, and syntax utilities.
function createBarrelFiles(
paths: string[],
options: { typeOnly: boolean; postfix?: string; moduleSuffix?: string }
): FileDescription[];
interface FileDescription {
path: string;
contents: string;
}Command-line interface for running TypeChain with extensive configuration options and file pattern matching.
interface ParsedArgs {
files: string[];
target: string;
outDir?: string;
inputDir?: string;
flags: {
discriminateTypes: boolean;
alwaysGenerateOverloads: boolean;
tsNocheck: boolean;
node16Modules: boolean;
};
}
function parseArgs(): ParsedArgs;Rich type system for representing EVM types, function signatures, and contract structures with full TypeScript integration.
type EvmType = BooleanType | IntegerType | UnsignedIntegerType | StringType |
BytesType | DynamicBytesType | AddressType | ArrayType | TupleType | UnknownType;
type EvmOutputType = EvmType | VoidType;
interface FunctionDeclaration {
name: string;
stateMutability: StateMutability;
inputs: AbiParameter[];
outputs: AbiOutputParameter[];
documentation?: FunctionDocumentation;
}Comprehensive file system utilities for path manipulation, globbing, and cross-platform compatibility.
function glob(cwd: string, patternsOrFiles: string[], ignoreNodeModules?: boolean): string[];
function detectInputsRoot(allFiles: string[]): string;
function normalizeName(rawName: string): string;Functions for generating contract function and event signatures for ABI compatibility and tooling.
function getSignatureForFn(fn: FunctionDeclaration): string;
function getFullSignatureForEvent(event: EventDeclaration): string;
function getFullSignatureAsSymbolForEvent(event: EventDeclaration): string;