Skip to content

Commit b2e9f66

Browse files
authored
docs(codec): avoid repeating type declare (#359)
1 parent 4329413 commit b2e9f66

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

packages/codec/README.md

+21-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ graph TD;
1616
import { struct, Uint8, Uint128 } from "@ckb-lumos/codec";
1717

1818
// udt-info.mol
19-
// table UDTInfo {
19+
// struct UDTInfo {
2020
// total_supply: Uint128,
2121
// decimals: Uint8,
2222
// }
@@ -187,3 +187,23 @@ table OmniLockWitnessLock {
187187
```
188188

189189
![](./assets/suggest-trigger.gif)
190+
191+
## Works with TypeScript
192+
193+
### Get Type Definition from Value
194+
195+
```ts
196+
import { molecule } from "@ckb-lumos/codec";
197+
import type { UnpackResult } from "@ckb-lumos/codec";
198+
199+
const { struct } = molecule;
200+
201+
const RGB = struct(
202+
{ r: Uint8, g: Uint8, b: Uint8 },
203+
["r", "g", "b"] // order of the keys needs to be consistent with the schema
204+
);
205+
206+
// We don't need to repeat the definition like this
207+
// type UnpackedRGB = { r: number; g: number; b: number };
208+
type UnpackedRGB = UnpackResult<typeof RGB>;
209+
```

0 commit comments

Comments
 (0)