📦 Ts Sdk Types — TS SDK 类型
v1.0.0迁移至 @aptos-labs/ts-sdk 的 TypeScript 类型映射:u64/u128/u256 为 bigint,address 为 string,TypeTag、functionArguments 与 typeArguments。触发条件:...
详细分析 ▾
运行时依赖
版本
ts-sdk-types 技能首发:使用 @aptos-labs/ts-sdk 将 Move 类型映射到 TypeScript 的清晰指南。 - 文档化类型映射:u64/u128/u256 → bigint,address → string,带 typeArguments 的泛型函数,以及 functionArguments 匹配。 - 说明如何处理 TypeTag、视图返回类型及 Move ↔ TypeScript 转换中的常见陷阱。 - 提供简洁表格与示例,覆盖所有常用 Move 类型到对应 TypeScript 表示的映射。 - 强调最佳实践与需避免的误用(如绝不用 number 表示 u128/u256)。 - 给出相关 SDK 模块及关联技能的引用。
安装命令
点击复制技能文档
Move → TypeScript(速览)
| Move 类型 | TypeScript 类型 | 示例 |
| ------------------- | -------------------------- | ---------------------------------------------------- |
| u8, u16, u32 | number | 255, 65535 |
| u64 | number \| bigint | 大值优先 bigint |
| u128, u256 | bigint | BigInt("340282366920938463463374607431768211455") |
| i8..i64 (Move 2.3+) | number \| bigint | i64 大值用 bigint |
| i128, i256 | bigint | BigInt("-...") |
| bool | boolean | true |
| address | string | "0x1" |
| signer | — | TS 不传入;signer 即交易发送者 |
| vector | Uint8Array \| string (hex) | new Uint8Array([1,2,3]) 或 hex |
| vector | T[] | [1, 2, 3] |
| String | string | "hello" |
| Object | string (对象地址) | objectAddress.toString() |
| Option | T \| null | 值或 null |
---
functionArguments
顺序与类型须与 Move entry/view 函数参数一致: ``typescript
// Move: public fun transfer(to: address, amount: u64)
await aptos.transaction.build.simple({
sender: account.accountAddress,
data: {
function: "0x1::coin::transfer",
typeArguments: ["0x1::aptos_coin::AptosCoin"],
functionArguments: [
"0xrecipient...", // address 用 string
1000n // u64 用 bigint(小值可用 number)
]
}
});
` ---
typeArguments
泛型 Move 函数传完整类型字符串(address::module::StructName):
`typescript
// Move: balance(addr): u64
typeArguments: ["0x1::aptos_coin::AptosCoin"]; // Move: transfer(to, amount)
typeArguments: ["0x1::aptos_coin::AptosCoin"];
`
---
View 返回类型
`typescript
const result = await aptos.view({
payload: {
function: "0x1::coin::balance",
typeArguments: ["0x1::aptos_coin::AptosCoin"],
functionArguments: [accountAddress]
}
});
// result 为数组;u128 常以 string 形式出现在 JSON
const balance = BigInt(result[0] as string);
` ---
TypeTag(进阶)
程序化构造 payload 或解析类型字符串时:
`typescript
import { TypeTag } from "@aptos-labs/ts-sdk";
import { parseTypeTag } from "@aptos-labs/ts-sdk"; const tag = parseTypeTag("0x1::aptos_coin::AptosCoin");
`
简单场景用 typeArguments 字符串数组;SDK API 要求时用 TypeTag。
---
Object / 资源地址入参
对象地址按 string 传入(AIP-40 长或短格式):
`typescript
functionArguments: [
nftObjectAddress.toString(), // 或 "0x..."
price
];
`` ---