Skip to content

Arrays ​

Typeflow has no loops — array work is done with four postfix constructs (distribution, [filter], [index], ^(sort)) plus projection and the aggregate functions.

OperatorEffect
.distribute a property access over array elements
[bool]keep elements matching a boolean predicate
[number]access an element by index
^(key)sort elements by one or more keys
count / sum / joincollapse an array to a scalar

. : distribute a property access over array elements ​

Accessing a property on an array of objects distributes over the elements — labels.name on { name: string }[] yields string[].

typeflow
names: user.labels.name

[bool] : keep elements matching a boolean predicate ​

A boolean expression in brackets keeps matching elements. Inside the brackets you are in element scope: bare identifiers resolve on the element. The predicate must be boolean — user.labels[name] is rejected (TF2009).

typeflow
active: user.labels[active]
activeNames: user.labels[active].name

[number] : access an element by index ​

Indexing yields element | undefined (the array may be shorter), so further access needs ?..

typeflow
first: user.labels[0]
firstName: user.labels[0]?.name
firstOr: user.labels[0]?.name ?? "none"

^(key) : sort elements by one or more keys ​

Sorts a copy of the array by each key in turn. Keys are expressions in element scope; prefix a key with > for descending (< or nothing for ascending). A key must be a number or a string (TF2011); nullish keys sort last.

typeflow
alphabetical: user.labels^(name).name
bestFirst: user.labels^(>score, name)

count / sum / join : collapse an array to a scalar ​

See Functions for the full aggregate list.

typeflow
games: count(stats.scores)
total: sum(stats.scores)
tagLine: join(stats.tags, ", ")

Playground ​

Mapping✔Playground ↗
Input JSON
Output
{
  "games": 3,
  "total": 42,
  "tagLine": "math, pioneer"
}
inferred type{ games: number; total: number; tagLine: string }

Next: Projection.

Released under the MIT License.