Errors are values
A function that may fail says so in its type. ? hands the error on, and nothing is thrown.
function quarter(n: number): Result<number, "odd" | "negative"> {
if (n < 0) return Err("negative")
Ok(half(half(n)?)?)
}strict-script is a small language that compiles to plain JavaScript. What can go wrong is written in the types, and the compiler makes you deal with it before the program runs, not in production.
A first kernel runs. It is not a release, and it is not proven. →
main.sts:5:3 type-mismatch This is `Result<unknown, JsonError>`, and `Invoice` is asked for. help: the call may fail: read it with `match`, or hand the error on with `?`
JSON.parse may fail, and so may reading an invoice out of what it gives. Both are in the type of the function, and ? hands them on.
Does it work? We try to break it. We keep a list of small TypeScript programs that compile without a warning and go wrong when they run: a value that is not what its type says, a forgotten null, a crash nobody expected. Then we try to write each one in strict-script.
Stopping every program of a list does not prove there is no other. Known limits says what is left.
How it works
It reads like TypeScript, with fewer words: no null, no undefined, no throw, no any, no cast, no class.
A function that may fail says so in its type. Forget an error, a missing value or a case, and the compiler refuses, with a line that says what to write instead.
Each file becomes a .js module and its .d.ts. TypeScript, Vite, React and Node import it as any module.
What you get
There is no null and no undefined. What may be missing is an Option, and each use says what happens when it is.
Nothing is thrown. A function that may fail returns a Result, and ? hands the error on in one character.
No any and no cast. What comes from npm, the DOM or Node is read against its type as the program runs.
It compiles to plain JavaScript with TypeScript declarations: put one file of it in a project you already have.
Fewer words
Each of these is a way a JavaScript program goes wrong. None is a word of strict-script: write one, and the compiler says what to write instead. Five words are new.
anyasnullundefinedthrowtrycatchclassthisnewtypeofinstanceofindeletevarswitchenummatchmutresourceextern?A tour
Six short samples. Each links to the page « The language », which says the rest.
A function that may fail says so in its type. ? hands the error on, and nothing is thrown.
function quarter(n: number): Result<number, "odd" | "negative"> {
if (n < 0) return Err("negative")
Ok(half(half(n)?)?)
}It gives what it found a name that cannot change. Nothing is narrowed by a test.
match →function area(shape: Shape): number {
match shape {
{ kind: "circle", radius } => 3.14 * radius * radius,
{ kind: "rect", width, height } => width * height,
}
}
match parseInvoice(text) {
Ok(invoice) => total(invoice),
Err({ kind: "json", message }) => 0,
Err(_) => -1,
}What can be changed has one owner: its name. Given away, it is a value nobody changes any more.
Values that can be changed →const mut basket: Item[] = []
basket.push(pen)
const mut counts: Map<string, number> = Map.from([])
counts.set("pen", 1)
const mut state = { sum: 0, count: 0 }
state.sum = state.sum + 5On every way out of it, return, ? and break among them, with no line written for the paths that fail.
export resource Transaction { entries: string[] } end rollback
export function begin(): Transaction { { entries: [] } }
export function debit(mut tx: Transaction, account: string, cents: number) { … }
export function commit(tx: Transaction): string[] { … }
export function rollback(tx: Transaction) { … }npm, the DOM and Node are named in an extern. Every call to them gives a Result.
extern "node:http" {
type Server
type IncomingMessage
type ServerResponse
function createServer(handler: (request: IncomingMessage, response: ServerResponse) => void): Server
function Server.listen(port: number): Server
const IncomingMessage.url: Option<string>
function ServerResponse.end(body: string): ServerResponse
}
extern {
type URL
function URL.new(href: string): URL
function fetch(url: string): Promise<Response>
}What may fail gives a Promise<Result<T, E>>, and await hands back a value.
async function size(path: string): Promise<Result<number, Thrown>> {
const text = await readFile(path, "utf8")?
Ok(text.length)
}
const sizes = await Promise.all(paths.map(path => size(path)))What comes out
This is what the compiler wrote for the file on the left when this page was built. The checks of what comes from outside are in the JavaScript: nobody writes a schema.
type Line = { label: string, quantity: number, unitPrice: number }
export type Invoice = { id: string, lines: Line[] }
export function total(invoice: Invoice): number {
invoice.lines.sum(line => line.quantity * line.unitPrice)
}
export function parseInvoice(json: string): Result<Invoice, JsonError | ParseError> {
Invoice.parse(JSON.parse(json)?)
}import * as $std from "@strict-script/std";
const $take_Invoice = /*#__PURE__*/ $std.check.lazy(() => $std.check.record({ id: $std.check.string, lines: $std.check.array($std.check.lazy(() => $take_Line)) }));
const $take_Line = /*#__PURE__*/ $std.check.lazy(() => $std.check.record({ label: $std.check.string, quantity: $std.check.number, unitPrice: $std.check.number }));
const $check_Invoice = /*#__PURE__*/ $std.check.lazy(() => $std.check.record({ id: $std.check.string, lines: $std.check.array($std.check.lazy(() => $check_Line)) }));
const $check_Line = /*#__PURE__*/ $std.check.lazy(() => $std.check.record({ label: $std.check.string, quantity: $std.check.number, unitPrice: $std.check.number }));
function total(invoice) {
return $std.sumBy(invoice.lines, (line) => line.quantity * line.unitPrice);
}
const $out_total = /*#__PURE__*/ $std.entry("total", total, () => [[$std.check.lazy(() => $take_Invoice)], $std.give.same]);
export { $out_total as total, total as $total };
function parseInvoice(json) {
const $t1 = $std.parseJson(json);
if (!$t1.ok) return $t1;
return $std.parse($std.check.lazy(() => $check_Invoice), $t1.value);
}
const $out_parseInvoice = /*#__PURE__*/ $std.entry("parseInvoice", parseInvoice, () => [[$std.check.string], $std.give.data]);
export { $out_parseInvoice as parseInvoice, parseInvoice as $parseInvoice };import type { Result, ParseError, JsonError } from "@strict-script/std";
type Line = { readonly label: string; readonly quantity: number; readonly unitPrice: number };
export type Invoice = { readonly id: string; readonly lines: readonly Line[] };
export declare function total(invoice: Invoice): number;
export declare function parseInvoice(json: string): Result<Invoice, JsonError | ParseError>;A .sts file built beside the TypeScript sources is imported as any module, with its types. An Option and a Result are plain data that if narrows.
What TypeScript hands to that module is not checked when the program runs: in a project says what that means.
import { parseInvoice, total } from './invoice.js';
const read = parseInvoice(text);
if (read.ok) console.log(total(read.value));
else console.log(read.error.kind);Around it
Each says what it was tested with, and what it was not.
One module at a time: a .sts file becomes a .js and a .d.ts that TypeScript imports as any module.
A plugin for the build, and a Vue component whose script is strict-script, its state in cells.
A component from a setup and a view written in the language. No effects, no styles.
A loader and a transformer, tested with webpack and with Jest. Not run inside Next.
A transformer for Metro, tested by React Native’s own preset. Not run on a phone.
The state and the logic of a component, shown as signals. The class and the template stay TypeScript.
Colours, what is refused as it is typed, and the type under the cursor, in VS Code and over the Language Server Protocol. No completion.
check, build, run. One line per refusal, and a line that says what to write instead.
The scoreboard
They are the four of strict-ts, plus memory, which the engine gives. This table is the project's scoreboard.
| Promise | In strict-script | Status |
|---|---|---|
| Memory safety | The engine's garbage collector | Given by the engine |
| Safe typing | No any, cast, declare or narrowing by a test; a record is closed; what comes from outside is read against its type as the program runs | No way known: 31 attacks, none gets through, a test each |
| No null, errors in the types | Option, Result, ?, a match that covers every case; a call to the outside gives a Result; a promise never rejects | No way known but one: 29 attacks, a test each; a recursion without end is accepted |
| Explicit mutability | Read-only unless it says mut, at the declaration and at the call | No way known: 24 attacks, none gets through, a test each |
| Ownership | What can be changed has one owner; a resource is ended by its scope, on every way out | Built and tested, with no list of attacks. Not built: state shared by several parts of a program |
« No way known » has a narrow meaning here: no way is known to write, in the kernel, a value that differs from its
type or a failure that its type does not announce. The measure is the 84 files of strict-ts that compile under
strict TypeScript and break a promise when they are run: 83 cannot be written or are refused, each by a test
(packages/compiler/test/attacks.test.ts); 1 is accepted, a recursion without end, which stops the program. It is a
list and not a proof, each row is the attack written again in this language by who wrote the compiler, and the only
others who tried to break it are two agents and a fuzzer, not a person. The kernel is fuzzed: a machine writes
programs and runs the ones that compile, and a run of 1,200,000 found nothing after the three holes it had found
were closed. What is taken on trust is in Known limits.
Asked
No, and it is not a superset of TypeScript: a TypeScript file is not a strict-script file. It reads like one, with
fewer words and a few more (match, mut, resource, extern, ?). The checker for standard TypeScript is its
sibling, strict-ts, which is released and is the one to use on existing
code.
One module at a time: a .sts file becomes a .js and a .d.ts that TypeScript imports as any module. What
TypeScript hands to a function of that module is read against its type when the program runs, and refused in the
caller when it is something else: see In a project.
By declaring what it has in an extern. Every call to it gives a Result, and what
comes back is read against the declared type: a declaration that is wrong is an Err where the call is made.
You cannot. There is no any, no cast, no comment that turns a rule off and no unsafe directory: the unsafe part is
the runtime library, which is not written in the language.
No. The promises rest on a list of attacks, not on a proof, and nobody but its author has tried to break the kernel.
What is taken on trust, the JavaScript that is written, the engine, each extern, is in
Known limits.
Colours, in VS Code and the editors that read a TextMate grammar. A language server says what is refused where it is typed, gives the type under the cursor and offers what may be written there, in VS Code and in the editors that speak its protocol: In the editor.
In strict-script: the lexer, the parser, the checker and what writes the JavaScript are 7,300 lines of the language
they compile (packages/self). The compiler compiles itself, and the tests pass on what it wrote.
The runtime library and the command line are TypeScript.
Write a program that compiles and fails in a way its types do not say. The compiler is in the page, and nothing is sent anywhere.