Skip to content

Commit

Permalink
docs: Improve Javascript Anchor Types reference (#3344)
Browse files Browse the repository at this point in the history
  • Loading branch information
acheroncrypto authored Nov 2, 2024
1 parent ebbad72 commit a982942
Showing 1 changed file with 73 additions and 101 deletions.
174 changes: 73 additions & 101 deletions docs/src/pages/docs/javascript-anchor-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,132 +8,104 @@ This reference shows you how Anchor maps Rust types to JavaScript/TypeScript typ
---

{% table %}
* Rust Type
* JavaScript Type
* Type
* Rust
* TypeScript
* Example
* Note
---
* ## Boolean
* `bool`
* `boolean`
* ```javascript
await program
.methods
.init(true)
.rpc();
* ```typescript
true
```
---
* `u64/u128/i64/i128`
* `anchor.BN`
* ```javascript
await program
.methods
.init(new anchor.BN(99))
.rpc();
```
* [https://github.com/indutny/bn.js](https://github.com/indutny/bn.js )
---
* ## Integer
* `u8/u16/u32/i8/i16/i32`
* `number`
* ```javascript
await program
.methods
.init(99)
.rpc();
```
* ```typescript
99
```
---
* ## Big integer
* `u64/u128/i64/i128`
* `anchor.BN`
* ```typescript
new anchor.BN(99)
```
---
* ## Float
* `f32/f64`
* `number`
* ```javascript
await program
.methods
.init(1.0)
.rpc();
```
* ```typescript
1.0
```
---
* `Enum`
* `object`
* ```rust
enum MyEnum {
One,
Two { val: u32 },
Three(u8, i16),
};
* ## String
* `String`
* `string`
* ```typescript
"hello"
```
```javascript
// Unit variant
await program
.methods
.init({ one: {} })
.rpc();
// Named variant
await program
.methods
.init({ two: { val: 99 } })
.rpc();
// Unnamed (tuple) variant
await program
.methods
.init({ three: [12, -34] })
.rpc();
---
* ## Array
* `[T; N]`
* `Array<T>`
* ```typescript
[1, 2, 3]
```
---
* ## Vector
* `Vec<T>`
* `Array<T>`
* ```typescript
[1, 2, 3]
```
---
* ## Option
* `Option<T>`
* `T | null | undefined`
* `None`:
```typescript
null
```
`Some(val)`:
```typescript
42
```
---
* ## Struct
* `Struct`
* `{ val: {} }`
* `object`
* ```rust
struct MyStruct {
val: u16,
}
```
```javascript
await program
.methods
.init({ val: 99 })
.rpc();
```typescript
{ val: 99 }
```
---
* `[T; N]`
* `Array<T>`
* ```javascript
await program
.methods
.init([1, 2, 3])
.rpc();
* ## Enum
* `Enum`
* `object`
* ```rust
enum MyEnum {
One,
Two { val: u32 },
Three(u8, i16),
}
```
---
* `String`
* `string`
* ```javascript
await program
.methods
.init("hello")
.rpc();
Unit variant:
```typescript
{ one : {} }
```
---
* `Vec<T>`
* `Array<T>`
* ```javascript
await program
.methods
.init([1, 2, 3])
.rpc();
Named variant:
```typescript
{ two: { val: 99 } }
```

---
* `Option<T>`
* `T | null | undefined`
* ```javascript
// `null` for `None`
await program
.methods
.init(null)
.rpc();
// Non-nullish value for `Option<T>`
await program
.methods
.init(42)
.rpc();
Unnamed (tuple) variant:
```typescript
{ three: [12, -34] }
```
{% /table %}

0 comments on commit a982942

Please sign in to comment.