0
# Types and Interfaces
1
2
Rich type system for representing EVM types, function signatures, and contract structures with full TypeScript integration.
3
4
## Capabilities
5
6
### EVM Type System
7
8
#### EvmType Union
9
10
Discriminated union of all possible EVM types.
11
12
```typescript { .api }
13
type EvmType = BooleanType | IntegerType | UnsignedIntegerType | StringType |
14
BytesType | DynamicBytesType | AddressType | ArrayType | TupleType | UnknownType;
15
```
16
17
#### EvmOutputType Union
18
19
Like EvmType but includes void for function outputs.
20
21
```typescript { .api }
22
type EvmOutputType = EvmType | VoidType;
23
```
24
25
#### StructType Union
26
27
Types that represent structs (complex types).
28
29
```typescript { .api }
30
type StructType = ArrayType | TupleType;
31
```
32
33
### Basic EVM Types
34
35
#### BooleanType
36
37
Represents Solidity boolean type.
38
39
```typescript { .api }
40
interface BooleanType {
41
type: 'boolean';
42
originalType: string;
43
}
44
```
45
46
#### IntegerType
47
48
Represents Solidity signed integer types (int8, int16, ..., int256).
49
50
```typescript { .api }
51
interface IntegerType {
52
type: 'integer';
53
/** Bit size (8, 16, 32, ..., 256) */
54
bits: number;
55
/** Original Solidity type string */
56
originalType: string;
57
}
58
```
59
60
#### UnsignedIntegerType
61
62
Represents Solidity unsigned integer types (uint8, uint16, ..., uint256).
63
64
```typescript { .api }
65
interface UnsignedIntegerType {
66
type: 'uinteger';
67
/** Bit size (8, 16, 32, ..., 256) */
68
bits: number;
69
/** Original Solidity type string */
70
originalType: string;
71
}
72
```
73
74
#### StringType
75
76
Represents Solidity string type.
77
78
```typescript { .api }
79
interface StringType {
80
type: 'string';
81
originalType: string;
82
}
83
```
84
85
#### AddressType
86
87
Represents Solidity address type.
88
89
```typescript { .api }
90
interface AddressType {
91
type: 'address';
92
originalType: string;
93
}
94
```
95
96
### Bytes Types
97
98
#### BytesType
99
100
Represents Solidity fixed-size bytes types (bytes1, bytes2, ..., bytes32).
101
102
```typescript { .api }
103
interface BytesType {
104
type: 'bytes';
105
/** Size in bytes (1-32) */
106
size: number;
107
/** Original Solidity type string */
108
originalType: string;
109
}
110
```
111
112
#### DynamicBytesType
113
114
Represents Solidity dynamic bytes type.
115
116
```typescript { .api }
117
interface DynamicBytesType {
118
type: 'dynamic-bytes';
119
originalType: string;
120
}
121
```
122
123
### Complex Types
124
125
#### ArrayType
126
127
Represents Solidity array types (fixed and dynamic).
128
129
```typescript { .api }
130
interface ArrayType {
131
type: 'array';
132
/** Type of array elements */
133
itemType: EvmType;
134
/** Optional fixed size, undefined for dynamic arrays */
135
size?: number;
136
/** Original Solidity type string */
137
originalType: string;
138
/** Optional struct name for named types */
139
structName?: StructName;
140
}
141
```
142
143
#### TupleType
144
145
Represents Solidity tuple types (structs).
146
147
```typescript { .api }
148
interface TupleType {
149
type: 'tuple';
150
/** Tuple components with names and types */
151
components: EvmSymbol[];
152
/** Original Solidity type string */
153
originalType: string;
154
/** Optional struct name for named types */
155
structName?: StructName;
156
}
157
```
158
159
### Special Types
160
161
#### VoidType
162
163
Represents void return type for functions with no outputs.
164
165
```typescript { .api }
166
interface VoidType {
167
type: 'void';
168
}
169
```
170
171
#### UnknownType
172
173
Represents unknown or unsupported types.
174
175
```typescript { .api }
176
interface UnknownType {
177
type: 'unknown';
178
/** Original type string that couldn't be parsed */
179
originalType: string;
180
}
181
```
182
183
### Type Parsing
184
185
#### parseEvmType Function
186
187
Main EVM type parser function.
188
189
```typescript { .api }
190
/**
191
* Main EVM type parser function
192
* @param rawType - Raw Solidity type string (e.g., "uint256", "address[]")
193
* @param components - Optional tuple components for complex types
194
* @param internalType - Optional internal type for struct name extraction
195
* @returns Parsed EVM type object
196
*/
197
function parseEvmType(rawType: string, components?: EvmSymbol[], internalType?: string): EvmType;
198
```
199
200
**Usage Example:**
201
202
```typescript
203
import { parseEvmType } from "typechain";
204
205
// Basic types
206
const uint256 = parseEvmType("uint256");
207
// { type: 'uinteger', bits: 256, originalType: 'uint256' }
208
209
const address = parseEvmType("address");
210
// { type: 'address', originalType: 'address' }
211
212
// Array types
213
const addressArray = parseEvmType("address[]");
214
// { type: 'array', itemType: { type: 'address', ... }, originalType: 'address[]' }
215
216
// Tuple types
217
const tupleComponents = [
218
{ name: "user", type: parseEvmType("address") },
219
{ name: "amount", type: parseEvmType("uint256") }
220
];
221
const tuple = parseEvmType("tuple", tupleComponents);
222
```
223
224
### Struct Names
225
226
#### StructName Class
227
228
Represents a struct name with optional namespace for organizing complex types.
229
230
```typescript { .api }
231
class StructName {
232
/** Struct identifier */
233
readonly identifier: string;
234
/** Optional namespace for organization */
235
readonly namespace?: string;
236
237
/**
238
* Creates a new StructName
239
* @param identifier - Struct name identifier
240
* @param namespace - Optional namespace
241
*/
242
constructor(identifier: string, namespace?: string);
243
244
/**
245
* String representation of the struct name
246
* @returns Formatted struct name
247
*/
248
toString(): string;
249
250
/**
251
* Merge with another StructName
252
* @param other - Partial StructName to merge
253
* @returns New merged StructName
254
*/
255
merge(other: Partial<StructName>): StructName;
256
}
257
```
258
259
#### extractStructNameIfAvailable Function
260
261
Extracts struct name from internal type string.
262
263
```typescript { .api }
264
/**
265
* Extracts struct name from internal type string
266
* @param internalType - Internal type from ABI (e.g., "struct MyContract.User")
267
* @returns StructName if extractable, undefined otherwise
268
*/
269
function extractStructNameIfAvailable(internalType: string | undefined): StructName | undefined;
270
```
271
272
### Named Types
273
274
#### EvmSymbol Type
275
276
Named EVM type combining name and type information.
277
278
```typescript { .api }
279
interface EvmSymbol {
280
/** Symbol name */
281
name: string;
282
/** EVM type */
283
type: EvmType;
284
}
285
```
286
287
#### Named Generic Type
288
289
Generic type for named values.
290
291
```typescript { .api }
292
interface Named<T> {
293
/** Item name */
294
name: string;
295
/** Item values */
296
values: T;
297
}
298
```
299
300
### Type Utilities
301
302
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:
303
304
**Usage Example:**
305
306
```typescript
307
import { parseEvmType, EvmType } from "typechain";
308
309
function processType(rawType: string) {
310
const evmType = parseEvmType(rawType);
311
312
// Use TypeScript's discriminated union type narrowing
313
if (evmType.type === 'array') {
314
console.log(`Array of ${evmType.itemType.type}`);
315
if (evmType.size) {
316
console.log(`Fixed size: ${evmType.size}`);
317
}
318
} else if (evmType.type === 'tuple') {
319
console.log(`Tuple with ${evmType.components.length} components`);
320
evmType.components.forEach(component => {
321
console.log(`- ${component.name}: ${component.type.type}`);
322
});
323
} else if (evmType.type === 'uinteger') {
324
console.log(`Unsigned integer with ${evmType.bits} bits`);
325
}
326
}
327
```