Rust to JS Type Conversion
Reference for how Anchor converts between Rust and TypeScript types
This reference shows to converts between Rust and TypeScript types.
Primitive Types
Boolean
Rust | TypeScript | Example |
---|---|---|
bool | boolean | true |
Numbers
Rust | TypeScript | Example |
---|---|---|
u8/u16/u32/i8/i16/i32 | number | 99 |
u64/u128/i64/i128 | anchor.BN | new anchor.BN(99) |
f32/f64 | number | 1.0 |
Strings
Rust | TypeScript | Example |
---|---|---|
String | string | "hello" |
Collections
Arrays and Vectors
Rust | TypeScript | Example |
---|---|---|
[T; N] (fixed array) | Array<T> | [1, 2, 3] |
Vec<T> (vector) | Array<T> | [1, 2, 3] |
Optional Values
Rust | TypeScript | Example |
---|---|---|
Option<T> | T | null | undefined | null (None)42 (Some) |
Complex Types
Structs
Enums
Notes
- Rust integers (
u8
throughi32
) map to JavaScriptnumber
- Larger integers (
u64
and above) use Anchor'sBN
type for precision - Rust's
Option<T>
maps to TypeScript's union type withnull
/undefined
- Structs and enums become JavaScript objects