Skip to content

Commit 61d8e90

Browse files
committed
update Tuple docs to include an example
1 parent f16c37d commit 61d8e90

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

docs/tuples.md

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,36 @@
11
# Tuples
22

3-
In TypeScript, tuples are a compact format for data structures. They're like fixed-length arrays
4-
that only contain the type. This is especially useful when using JSON, as including property names
5-
means the messages are much larger.
3+
In TypeScript,
4+
[tuples](https://www.typescriptlang.org/docs/handbook/2/objects.html#tuple-types)
5+
are a compact format for data structures. They're like fixed-length arrays that only contain the
6+
type, not the property names. Excluding the property names is especially useful when size and speed
7+
is important, because the JSON will be much more compact.
8+
9+
### Tuple example
10+
11+
Here's an example of a tuple definition in TypeScript:
12+
13+
```typescript
14+
type StringNumberPair = [str: string, num: number];
15+
```
16+
17+
This would get serialized to a JSON array
18+
19+
[//]: # (@formatter:off)
20+
```json
21+
["some string value", 123]
22+
```
23+
[//]: # (@formatter:on)
24+
25+
which is more compact than an equivalent JSON object, which requires property names.
26+
27+
[//]: # (@formatter:off)
28+
```json
29+
{ "str": "some string value", "num": 123 }
30+
```
31+
[//]: # (@formatter:on)
32+
33+
## Tuples in KxsTsGen
634

735
Tuples are a bit difficult to create in Kotlinx Serialization, but KxsTsGen includes
836
[TupleSerializer](../modules/kxs-ts-gen-core/src/commonMain/kotlin/dev/adamko/kxstsgen/core/experiments/tuple.kt)

0 commit comments

Comments
 (0)