Status: implemented
A schema is an ordinary immutable value. A Type is one of:
Const, used directly as its own schema:Primitive(null,undefined,true,42,'hello',7n), which admits exactly itself (constants)Tuple(readonly[schema, ...]), which reads each element by positionStruct({ key: schema, ... }), which reads each declared property
Thunk(() => Info), a lazy schema for the built-ins and for recursive types
The built-in schemas are all thunks: functions that return an Info descriptor. Nullary schemas (boolean, string, etc.) return a single-tag tuple; unary schemas (array, record) return a tag and the inner type.
| Schema | Returns | Admits | Page |
|---|---|---|---|
boolean | ['boolean'] | any boolean | primitives |
number | ['number'] | any number | primitives |
string | ['string'] | any string | primitives |
bigint | ['bigint'] | any bigint | primitives |
unknown | ['unknown'] | any DataJS value | primitives |
option | ['option'] | nothing but absence: or(option, t) is a member that may be left out | absence |
or(...ts) | ['or', ...ts] | a value any member admits; never is or() | unions |
array(t) | ['array', t] | readonly Ts<t>[] | arrays and records |
record(t) | ['record', t] | { readonly[K in string]?: Ts<t> } | arrays and records |
rest(c, r) | ['rest', c, r] | c's members, and only members of r besides | open containers |
open(c) | ['rest', c, unknown] | c's members, and anything else | open containers |
- Constants: a primitive as its own schema
- Primitives:
boolean,number,string,bigint,unknown - Structs and tuples are closed: the members a container declares and no others
- Open containers:
openandrest - Absence:
option, and why it is notundefined - Unions:
orandnever - Arrays and records: homogeneous containers
- Recursion and sharing: a schema that uses itself, and one used in two places
These TypeScript type forms have no schema yet:
- function types (668)
- a string type that excludes some values (excluded string values)
- intersections, which no todo proposes yet
- TypeScript: everyday types and object types
- JSON Schema: the reference, draft 2020-12, the draft
toJsonSchemawrites
The constructors in module.f.mjs and the types in types.ts.