Type checking

Rook — 1.0 specification

The compiler checks types before generating JS; a program with type errors does not compile (rook build --no-check skips the check).

hello.rk:4:17: error: cannot use ?User as User: it may be null, check it first
hello.rk:7:13: error: "u" may be null: check it with "if u != null" or use "?."
hello.rk:9:11: error: cannot add string and number: use a template string, e.g. `${a}${b}`

What is checked

Narrowing

The type of a local variable or a parameter narrows from ?T to T, and so does the type of a path of fields from one, order.delivery (see below):

Unions narrow the same way:

An assignment to a variable ends its narrowing. In a loop, variables that change in it are not narrowed. In a closure, narrowing is kept only for constants and variables that are never reassigned.

Paths. A path is a chain of .field from a local variable, a parameter, state, derived or a const of the module: order.delivery, response.data.user. It narrows the same ways as a variable, without an intermediate const:

A call of a function that changes the object does not end the narrowing of its paths, as in TypeScript: after if order.delivery.kind == "courier" { reset(order) } the compiler still considers order.delivery a Courier. This is a deliberate trade-off for convenience: a function that replaces a field of an object it is given is rare, and checking every call would undo the narrowing almost everywhere.

Type inference

Built-in JS types

By default, the compiler uses TypeScript's lib files (ES2025 and DOM) and the global @types packages available to the project. For example, @types/node provides process and Buffer:

TypeScript supplies the signatures and overloads for console, Math, JSON, timers and the methods of strings, numbers and arrays. Results that can be undefined in TypeScript, such as find, pop and at, are nullable in Rook. Rook provides its own declarations for the predeclared functions and the Error class of its error type.

The playground uses standard-library and DOM types generated from TypeScript declarations by npm run browser:types. The browser loads a compact data set that includes document, fetch, Map, Promise, timers and streams. Large interfaces retain a selected set of members. Results whose shape is unknown, such as Response.json(), have type unknown and can be checked with decode<T>.

The types from imported .rk modules are checked: the compiler reads those modules itself. The types of other imports come from TypeScript declarations (section 10).

Markdown source on GitHub