Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Improve Javascript Anchor Types reference #3344

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 %}
Loading