Rook
РусскийRU

Variables and types

Variables are declared as in JavaScript, with let and const, and the type goes after the name, as in Go. With a value, the type can be left out: the compiler infers it. In our board, a task is a record with an ID, title, done flag and priority; the chapters that follow use these fields to connect language features to the same app.

A let without a value gets the zero value of its type, as in Go: 0, "", false, an empty array or null.

The fields of an object go into variables as in JS, const { name, price: cost, ...rest } = guava, and a method in an object literal is written as in a class.

let count number           // without a value, the zero value of the type: 0
let title = "Guava"        // the type is inferred from the value: string
const prices number[] = [120, 90, 250]

let a, b = 1, 2
a, b = b, a                // swap the values

count += prices.length
console.log(count, title, a, b)

// The fields of an object go into variables, renamed ones and the rest too
const guava = { name: "Guava", price: 120, stock: 7 }
const { name, price: cost, ...rest } = guava
console.log(name, cost, rest.stock)

// A method in an object literal is written as in a class
const shop = {
    open: true,
    label(hour number) string {
        return hour < 20 ? "Open" : "Closed"
    },
}
console.log(shop.label(19))
TypeWhat it is
number, string, boolnumbers, strings and booleans
T[]an array of values of type T
?TT or null
func(number) stringa function with a number parameter and a string result
(name string) => boolthe same kind of function type, written as an arrow
interface User { ... }an object with the listed fields and methods
{ [string]: number }an object used as a map: any keys, number values
type ID numberanother name for a type
anyanything, unchecked: for example, values from JS
type ID number

interface User {
    id ID
    name string
    email ?string          // ?T: the value may be null
}

const users User[] = [
    { id: 1, name: "Ann", email: null },
    { id: 2, name: "Bob", email: "bob@example.com" },
]

for user in users {
    console.log(user.name, user.email ?? "no email")
}

An object used as a map has the type { [string]: T }: its keys can be any, and delete prices.lychee removes one. value as Type sees a value as a related type, for example what JSON.parse returns; unrelated types, such as count as string, are a compile error.

// { [string]: T } is an object used as a map: any key, values of type T
const prices { [string]: number } = { guava: 3, lychee: 7 }
prices["papaya"] = 4
delete prices.lychee                 // delete removes a key

// A function type can be written as an arrow: (name string) => string
const format (name string) => string = name => `${name}: €${prices[name]}`

for name in Object.keys(prices) {
    console.log(format(name))
}

// as sees a value as a related type: JSON.parse gives any
const saved = JSON.parse(`{ "guava": 5 }`) as { [string]: number }
console.log("saved:", saved["guava"])

Regular expressions are written as in JS, /ab+c/gi, and the compiler checks the pattern and the flags: /(/ is a compile error.

// A regular expression is written as in JS; the compiler checks the pattern and the flags
const date = /^(\d{4})-(\d{2})-(\d{2})$/
const text = "guava, papaya and lychee"

console.log(date.test("2026-10-26"), date.test("26.10.2026"))
console.log(text.replace(/a/g, "o"))
console.log(text.split(/,\s*|\s+and\s+/))