Arrays and records

Status: implemented

array(t) admits an array of any length whose every element is a t. record(t) admits an object with any string keys, each holding a t.

const tags = array(string)       // readonly string[]
const stock = record(number)     // { readonly [k in string]?: number }

Both are homogeneous. An array whose positions differ is a tuple, and an object with known keys is a struct; rest combines the two, declared members and then any number of members of one schema. array(or()) admits only the empty array, an empty rest bounding the length.

Examples

The demo on this page runs the Dictionary lesson: record(number) against {"apples":3,"pears":5}, with its graph, its TypeScript type, its JSON Schema, and what parse and validate make of the value. Change a count to a string to see both refuse it. The lesson is defined in sample.f.mjs, and the module page's demo lists it among all the others.

Reference

Where it is implemented

The unary schemas in module.f.mjs; the container skeletons in common.

Demo

record(t) admits any string keys, each holding a t.

record(number)
recordnumber

TypeScript:

{readonly[k in string]?:number}

JSON Schema:

{"type":"object","additionalProperties":{"type":"number"}}
Canonical form (toData)
export default [{},{"object":[{"props":{},"rest":{"number":true}}]}];

parse · ok

export default {"apples":3,"pears":5};

validate · ok

export default {"apples":3,"pears":5};

Emergent Testing

Idle. Press Run to start the suite.

  • ./proof.f.mjs