strict-script strict-script

The language

The standard library

It has two sides. In a .sts file nothing is imported: Option, Result, the collections and the functions of the platform come with the language. The JavaScript that is written imports @strict-script/std, a small runtime library with no dependency, and a TypeScript project that calls a compiled module reads its types from there.

#In the language

Option<T>Some(value), None; map, andThen, unwrapOr, okOr(error), isSome(), isNone(); option ?? fallback
Result<T, E>Ok(value), Err(error); map, mapErr, andThen, unwrapOr, ok(), isOk(), isErr()
value?The value of an Ok or of a Some; otherwise leaves the function with the Err or the None
Type.parse(value)Reads a value of unknown type against any named type: Result<Type, ParseError>. Nobody writes a schema
JSON.parse(text), JSON.stringify(value)JSON without exceptions: Result<unknown, JsonError>, and a text. Where the value holds a number, writing gives a Result<string, JsonError>: JSON has no word for NaN nor for an infinity, and none is written as null
An arraymap, filter, find, reduce(f, start), flatMap, sum, sortedBy, reversed, first, last, groupBy…; with mut: push, pop, set, clear, reverse, sortBy
Map<K, V>, Set<T>Map.from, Set.from; get gives an Option; set, add and without give another one, or change it in place on a name declared with mut
A text, a number, a biginttext.toNumber(), n.toFixed(digits), text.repeat(count), BigInt(n), and /, %, ** on bigints give an Option where JavaScript throws or gives NaN. A number read from outside is finite; one made by an operation (0 / 0) may not be: see Known limits
Promise<T>await, Promise.all, sleep(milliseconds), detach(promise). A promise never rejects
panic(message)For a bug, which is not an error to hand on: it stops the program, and nothing in the language catches it

Every method and every function is listed in The language.

#From TypeScript

import type { JsonError, Option, ParseError, Result, Thrown } from '@strict-script/std';
Option<T>{ some: true, value } | { some: false }; if (option.some) narrows
Result<T, E>{ ok: true, value } | { ok: false, error }; if (result.ok) narrows
some(value), none, ok(value), err(error)To make one to hand to a compiled function
JsonError{ kind: "json", message }
ParseError{ kind: "parse", path, expected }
Thrown{ kind: "thrown", name, message }: what a call to the outside threw, or rejected with

They are plain data, with no method and no class: a value made in the language and one made in TypeScript are the same. Records and arrays are typed read-only.

The rest of what the package exports (attempt, guard, check, mapGet, sortedBy…) is what the compiler calls in the JavaScript it writes. It is not meant to be called by hand, and its names may change with the compiler.

#To know

  • The package never throws to a compiled program, except panic and the stop of a match that was handed a value that is none of the shapes of its type.
  • It is not written in strict-script and is not under strict-ts: it is where exceptions are caught and values of unknown type are read. See Known limits.
Esc

    Type a word of the language, a rule, a command: mut, extern, match-not-covered.