CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-typechain

TypeScript bindings generator for Ethereum smart contracts that transforms ABI files into type-safe interfaces and classes

Pending
Overview
Eval results
Files

types-interfaces.mddocs/

Types and Interfaces

Rich type system for representing EVM types, function signatures, and contract structures with full TypeScript integration.

Capabilities

EVM Type System

EvmType Union

Discriminated union of all possible EVM types.

type EvmType = BooleanType | IntegerType | UnsignedIntegerType | StringType | 
               BytesType | DynamicBytesType | AddressType | ArrayType | TupleType | UnknownType;

EvmOutputType Union

Like EvmType but includes void for function outputs.

type EvmOutputType = EvmType | VoidType;

StructType Union

Types that represent structs (complex types).

type StructType = ArrayType | TupleType;

Basic EVM Types

BooleanType

Represents Solidity boolean type.

interface BooleanType {
  type: 'boolean';
  originalType: string;
}

IntegerType

Represents Solidity signed integer types (int8, int16, ..., int256).

interface IntegerType {
  type: 'integer';
  /** Bit size (8, 16, 32, ..., 256) */
  bits: number;
  /** Original Solidity type string */
  originalType: string;
}

UnsignedIntegerType

Represents Solidity unsigned integer types (uint8, uint16, ..., uint256).

interface UnsignedIntegerType {
  type: 'uinteger';
  /** Bit size (8, 16, 32, ..., 256) */
  bits: number;
  /** Original Solidity type string */
  originalType: string;
}

StringType

Represents Solidity string type.

interface StringType {
  type: 'string';
  originalType: string;
}

AddressType

Represents Solidity address type.

interface AddressType {
  type: 'address';
  originalType: string;
}

Bytes Types

BytesType

Represents Solidity fixed-size bytes types (bytes1, bytes2, ..., bytes32).

interface BytesType {
  type: 'bytes';
  /** Size in bytes (1-32) */
  size: number;
  /** Original Solidity type string */
  originalType: string;
}

DynamicBytesType

Represents Solidity dynamic bytes type.

interface DynamicBytesType {
  type: 'dynamic-bytes';
  originalType: string;
}

Complex Types

ArrayType

Represents Solidity array types (fixed and dynamic).

interface ArrayType {
  type: 'array';
  /** Type of array elements */
  itemType: EvmType;
  /** Optional fixed size, undefined for dynamic arrays */
  size?: number;
  /** Original Solidity type string */
  originalType: string;
  /** Optional struct name for named types */
  structName?: StructName;
}

TupleType

Represents Solidity tuple types (structs).

interface TupleType {
  type: 'tuple';
  /** Tuple components with names and types */
  components: EvmSymbol[];
  /** Original Solidity type string */
  originalType: string;
  /** Optional struct name for named types */
  structName?: StructName;
}

Special Types

VoidType

Represents void return type for functions with no outputs.

interface VoidType {
  type: 'void';
}

UnknownType

Represents unknown or unsupported types.

interface UnknownType {
  type: 'unknown';
  /** Original type string that couldn't be parsed */
  originalType: string;
}

Type Parsing

parseEvmType Function

Main EVM type parser function.

/**
 * Main EVM type parser function
 * @param rawType - Raw Solidity type string (e.g., "uint256", "address[]")
 * @param components - Optional tuple components for complex types
 * @param internalType - Optional internal type for struct name extraction
 * @returns Parsed EVM type object
 */
function parseEvmType(rawType: string, components?: EvmSymbol[], internalType?: string): EvmType;

Usage Example:

import { parseEvmType } from "typechain";

// Basic types
const uint256 = parseEvmType("uint256");
// { type: 'uinteger', bits: 256, originalType: 'uint256' }

const address = parseEvmType("address");
// { type: 'address', originalType: 'address' }

// Array types
const addressArray = parseEvmType("address[]");
// { type: 'array', itemType: { type: 'address', ... }, originalType: 'address[]' }

// Tuple types
const tupleComponents = [
  { name: "user", type: parseEvmType("address") },
  { name: "amount", type: parseEvmType("uint256") }
];
const tuple = parseEvmType("tuple", tupleComponents);

Struct Names

StructName Class

Represents a struct name with optional namespace for organizing complex types.

class StructName {
  /** Struct identifier */
  readonly identifier: string;
  /** Optional namespace for organization */
  readonly namespace?: string;

  /**
   * Creates a new StructName
   * @param identifier - Struct name identifier
   * @param namespace - Optional namespace
   */
  constructor(identifier: string, namespace?: string);

  /**
   * String representation of the struct name
   * @returns Formatted struct name
   */
  toString(): string;

  /**
   * Merge with another StructName
   * @param other - Partial StructName to merge
   * @returns New merged StructName
   */
  merge(other: Partial<StructName>): StructName;
}

extractStructNameIfAvailable Function

Extracts struct name from internal type string.

/**
 * Extracts struct name from internal type string
 * @param internalType - Internal type from ABI (e.g., "struct MyContract.User")
 * @returns StructName if extractable, undefined otherwise
 */
function extractStructNameIfAvailable(internalType: string | undefined): StructName | undefined;

Named Types

EvmSymbol Type

Named EVM type combining name and type information.

interface EvmSymbol {
  /** Symbol name */
  name: string;
  /** EVM type */
  type: EvmType;
}

Named Generic Type

Generic type for named values.

interface Named<T> {
  /** Item name */
  name: string;
  /** Item values */
  values: T;
}

Type Utilities

TypeChain provides runtime type checking through TypeScript's discriminated union system. Since all types have a type property, you can use standard TypeScript type narrowing:

Usage Example:

import { parseEvmType, EvmType } from "typechain";

function processType(rawType: string) {
  const evmType = parseEvmType(rawType);
  
  // Use TypeScript's discriminated union type narrowing
  if (evmType.type === 'array') {
    console.log(`Array of ${evmType.itemType.type}`);
    if (evmType.size) {
      console.log(`Fixed size: ${evmType.size}`);
    }
  } else if (evmType.type === 'tuple') {  
    console.log(`Tuple with ${evmType.components.length} components`);
    evmType.components.forEach(component => {
      console.log(`- ${component.name}: ${component.type.type}`);
    });
  } else if (evmType.type === 'uinteger') {
    console.log(`Unsigned integer with ${evmType.bits} bits`);
  }
}

Install with Tessl CLI

npx tessl i tessl/npm-typechain

docs

abi-parsing.md

cli-interface.md

code-generation.md

core-api.md

file-utilities.md

index.md

types-interfaces.md

tile.json