strict-script strict-script

Start

Getting started

#What it looks like

Two functions written as they often are in TypeScript, the cast left out since there is no word for it:

type Invoice = { id: string, total: number }

export function parseInvoice(json: string): Invoice {
  JSON.parse(json)
}

export function label(status: "draft" | "sent" | "paid"): string {
  match status {
    "draft" => "Draft",
    "sent" => "Sent",
  }
}
invoice.sts:4: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 `?`
invoice.sts:8:3 match-not-covered `match` does not cover `"paid"`.
  help: add an arm for it, or `_ => …` for everything else

2 refused in 1 file(s)

The text may not be JSON, and the JSON may not be an invoice: both are said by the type of the function, and ? hands them on. Writing as Invoice is refused too, as a word of JavaScript that has no meaning here.

export function parseInvoice(json: string): Result<Invoice, JsonError | ParseError> {
  Invoice.parse(JSON.parse(json)?)
}

export function label(status: "draft" | "sent" | "paid"): string {
  match status {
    "draft" => "Draft",
    "sent" => "Sent",
    "paid" => "Paid",
  }
}

Invoice.parse is not written by anyone: every type that is named reads a value of unknown type against itself.

#Try it

corepack enable        # once: gives pnpm
pnpm install
pnpm build
ln -s "$PWD/packages/cli/dist/main.js" ~/.local/bin/strict-script
                       # once: the command, in a folder of the PATH

strict-script run examples/invoice/main.sts             # prints three lines, exit code 0
strict-script run examples/invoice/main.sts '{"id":1}'  # not an invoice: at id, expected a text
strict-script check examples                            # exit code 0, nothing reported
strict-script build examples/invoice --out out          # out/invoice.js, .d.ts, .js.map, and main

The link points into the clone, so the command is the compiler of the last pnpm build, from any folder. Without it, pnpm strict-script … runs the same command from the clone.

run builds the file and what it imports, then calls its exported main with the arguments that follow. A main that gives an Err prints it, and the exit code is 1.

#A file of your own

// hello.sts
export function main(args: string[]): Result<void, string> {
  const name = args[0] ?? "world"
  if (name.length == 0) return Err("a name is not empty")
  console.log(`Hello, ${name}!`)
  Ok()
}
strict-script run hello.sts Ada

Nothing runs at the top of a file: a program starts in main. args[0] is an Option<string>, since the array may be empty, and ?? gives what to use then. The language says the rest.

#Examples

Six small programs, compiled and run by the tests: examples/. The first four are the examples of strict-ts, written again.

ExampleAbout
invoiceTyping and errors: what comes from outside is read against its type, and every failure is in a type
reportWhat throws without saying so in JavaScript: JSON.parse, a pattern, a bigint division
cartExplicit mutability: who changes what, said at the declaration and at the call
transferOwnership: a transaction ended exactly once, with no line written for the paths that fail
wordcountThe file system as the outside, and a map that holds the counts
serverA web server: functions handed to the outside, values of the outside, promises

#The playground, and the website

The playground is a page of the website: the compiler in the page, with nine examples to change and run. Nothing is sent anywhere. While a name is typed, or after a ., it shows what may be written there, each with its type: the names that can be used, the properties of a record, the methods of a list or of a text, the cases of a match.

The website is a front page, these documents and the playground as one folder of files, which opens from the disk and loads nothing from elsewhere.

pnpm build && pnpm site            # writes packages/site/dist/
pnpm site:serve                    # shows it at http://127.0.0.1:4173/
Esc

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