Rebasely Beta: data may be purged

justintypescript-ultra-strict Public

Markdown · 4.0 KiB · at 6f4e421

Learnings from running the examples with no toolchain

Phase 1 was writing 26 example projects under examples/ and running them with stock tooling. Everything failed, in three distinct ways, and each failure dictated a piece of the MVP design.

Failure 1: the extension is a hard wall

node examples/01-hello/src/main.tsus and node --experimental-strip-types both die with ERR_UNKNOWN_FILE_EXTENSION before reading a single byte. Node's type-stripping only accepts .ts/.mts, so a new extension means we own the compile step end to end. There is no incremental "mostly works" path.

Consequence for the MVP: tsus build must exist and must emit plain .js before anything runs. Compiled output has to be standard ESM so Node and Cloudflare Workers can consume it with zero knowledge of .tsus.

Failure 2: match blocks are not parseable TypeScript

Renaming 04-match-option/src/main.tsus to .ts and running tsc --noEmit produces TS1005: ',' expected and TS1109: Expression expected at the match arms. The TS parser cannot recover into anything useful, so we cannot implement match as a tsc plugin or an AST transform over the original source.

Consequence: the MVP needs a preprocessor that runs before the TypeScript parser, scanning raw text and rewriting match (expr) { Pattern(x) => body } into a plain function call (__tsus_match). The scanner has to be a real tokenizer that understands strings, template literals (including nested ${}), and comments, because example 24 deliberately plants fake match blocks inside all of those. A regex will not survive it.

Failure 3: import specifiers with .tsus extensions break resolution

Even with compiled output, import { safeSqrt } from "./math.tsus" would still fail at runtime because nothing on disk has that name after compilation. The examples with import chains (12, 20, 21, 22) all hit this.

Consequence: the build step rewrites .tsus and .ts specifiers to .js in emitted code, and compiles sibling .ts files too, so a mixed ts+tsus project comes out as one coherent dist tree.

Smaller findings baked into the design

  • await inside a match arm: arms compile to arrow functions, so an arm whose body contains await must become an async arrow, and __tsus_match then returns a promise the caller awaits. Example 19 forces this.
  • "abc".match(/\d+/) and a user function literally named match (example 25) must not trigger the macro. The rewrite only fires on match (expr) { where the token is not preceded by . or an identifier character, and only when a { follows the closing paren.
  • Bare string-literal patterns ("/" => ... in the worker example) mean patterns are not just variant tags; the runtime match needs literal equality patterns plus a _ wildcard.
  • Circular .tsus imports (example 21) are fine as long as the cycle is function-level, because compiled output is ESM and ESM handles cycles with hoisted bindings. The compiler does not need to detect cycles for the MVP.
  • The checker rules (no class, no null, no undefined, no var, no throw) apply to .tsus files only. Examples 13 and 22 have .ts dependencies that use classes, null, and undefined on purpose; the checker must scope itself by extension or mixed projects are impossible.

Outcome

With the MVP in place (packages/runtime + packages/cli), the harness reports 26/26 examples passing, including the five that pass by being rejected with the expected diagnostic code. Known MVP limitations, in honesty order:

  1. No semantic type-checking yet: tsus build strips types per-module (ts.transpileModule), so type errors surface at runtime. The next step is a ts.createProgram pass with a virtual file system that maps .tsus to preprocessed TS.
  2. No exhaustiveness checking: a non-exhaustive match throws TSUS100 at runtime instead of failing at compile time.
  3. The await-in-arm detection is textual, so a string literal containing the word await would wrongly make that arm async.
  4. Regex literals are not tokenized; a regex containing a quote or brace character could confuse the scanner.