Rebasely Beta: data may be purged

justintypescript-ultra-strict Public

Merge pull request #1 from chitalian/mvp

Mvp
0eb2eae9951132100dcbb79a5408e5e7b9295376
Parents dcef8e1, 6f4e421 Browse filesHistory

Changed files

81 files changed415 additions0 deletionsagainst dcef8e1

  1. Added.gitignore+3−0
  2. AddedLEARNINGS.md+38−0
  3. ChangedREADME.md+2−0
  4. Addedexamples/01-hello/example.json+1−0
  5. Addedexamples/01-hello/src/main.tsus+3−0
  6. Addedexamples/02-option-basics/example.json+1−0
  7. Addedexamples/02-option-basics/src/main.tsus+14−0
  8. Addedexamples/03-result-basics/example.json+1−0
  9. Addedexamples/03-result-basics/src/main.tsus+9−0
  10. Addedexamples/04-match-option/example.json+1−0
  11. Addedexamples/04-match-option/src/main.tsus+13−0
  12. Addedexamples/05-match-result/example.json+1−0
  13. Addedexamples/05-match-result/src/main.tsus+22−0
  14. Addedexamples/06-tagged-union/example.json+1−0
  15. Addedexamples/06-tagged-union/src/main.tsus+21−0
  16. Addedexamples/07-banned-null/example.json+1−0
  17. Addedexamples/07-banned-null/src/main.tsus+3−0
  18. Addedexamples/08-banned-class/example.json+1−0
  19. Addedexamples/08-banned-class/src/main.tsus+5−0
  20. Addedexamples/09-banned-var/example.json+1−0
  21. Addedexamples/09-banned-var/src/main.tsus+4−0
  22. Addedexamples/10-banned-undefined/example.json+1−0
  23. Addedexamples/10-banned-undefined/src/main.tsus+3−0
  24. Addedexamples/11-banned-throw/example.json+1−0
  25. Addedexamples/11-banned-throw/src/main.tsus+5−0
  26. Addedexamples/12-tsus-imports-tsus/example.json+1−0
  27. Addedexamples/12-tsus-imports-tsus/src/main.tsus+4−0
  28. Addedexamples/12-tsus-imports-tsus/src/math.tsus+6−0
  29. Addedexamples/13-tsus-imports-ts/example.json+1−0
  30. Addedexamples/13-tsus-imports-ts/src/legacy.ts+19−0
  31. Addedexamples/13-tsus-imports-ts/src/main.tsus+11−0
  32. Addedexamples/14-ts-imports-tsus/example.json+1−0
  33. Addedexamples/14-ts-imports-tsus/src/lib.tsus+9−0
  34. Addedexamples/14-ts-imports-tsus/src/main.ts+9−0
  35. Addedexamples/15-npm-dependency/example.json+1−0
  36. Addedexamples/15-npm-dependency/src/main.tsus+26−0
  37. Addedexamples/16-boundary-fs/example.json+1−0
  38. Addedexamples/16-boundary-fs/src/data.txt+1−0
  39. Addedexamples/16-boundary-fs/src/main.tsus+13−0
  40. Addedexamples/17-nested-match/example.json+1−0
  41. Addedexamples/17-nested-match/src/main.tsus+24−0
  42. Addedexamples/18-generics/example.json+1−0
  43. Addedexamples/18-generics/src/main.tsus+25−0
  44. Addedexamples/19-async-result/example.json+1−0
  45. Addedexamples/19-async-result/src/main.tsus+26−0
  46. Addedexamples/20-deep-chain/example.json+1−0
  47. Addedexamples/20-deep-chain/src/b.tsus+4−0
  48. Addedexamples/20-deep-chain/src/c.tsus+3−0
  49. Addedexamples/20-deep-chain/src/d.tsus+1−0
  50. Addedexamples/20-deep-chain/src/main.tsus+6−0
  51. Addedexamples/21-circular-imports/example.json+1−0
  52. Addedexamples/21-circular-imports/src/even.tsus+3−0
  53. Addedexamples/21-circular-imports/src/main.tsus+6−0
  54. Addedexamples/21-circular-imports/src/odd.tsus+3−0
  55. Addedexamples/22-mixed-project/example.json+1−0
  56. Addedexamples/22-mixed-project/src/main.tsus+14−0
  57. Addedexamples/22-mixed-project/src/repo.tsus+7−0
  58. Addedexamples/22-mixed-project/src/store.ts+8−0
  59. Addedexamples/22-mixed-project/src/validate.tsus+11−0
  60. Addedexamples/23-cloudflare-worker/example.json+9−0
  61. Addedexamples/23-cloudflare-worker/src/router.tsus
  62. Addedexamples/23-cloudflare-worker/src/worker.tsus
  63. Addedexamples/24-hostile-syntax/example.json
  64. Addedexamples/24-hostile-syntax/src/main.tsus
  65. Addedexamples/25-shadowed-match/example.json
  66. Addedexamples/25-shadowed-match/src/main.tsus
  67. Addedexamples/26-json-boundary/example.json
  68. Addedexamples/26-json-boundary/src/main.tsus
  69. Addedpackage-lock.json
  70. Addedpackage.json
  71. Addedpackages/cli/bin/tsus.js
  72. Addedpackages/cli/package.json
  73. Addedpackages/cli/src/build.js
  74. Addedpackages/cli/src/check.js
  75. Addedpackages/cli/src/preprocess.js
  76. Addedpackages/runtime/boundary.d.ts
  77. Addedpackages/runtime/boundary.js
  78. Addedpackages/runtime/index.d.ts
  79. Addedpackages/runtime/index.js
  80. Addedpackages/runtime/package.json
  81. Addedscripts/run-examples.mjs
Added.gitignore+3−0
@@ -0,0 +1,3 @@
+node_modules/
+dist/
+*.log
View file
AddedLEARNINGS.md+38−0
@@ -0,0 +1,38 @@
+# 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.
View file
ChangedREADME.md+2−0
@@ -2,6 +2,8 @@
This is my proposal for TypeScript-Ultra-Strict: a stricter TypeScript for a world where AI writes most of the code.
+> **Status: working MVP on the `mvp` branch.** `packages/cli` implements `tsus check` and `tsus build`, `packages/runtime` implements `Option`/`Result`/`match` and the boundary layer, and all 26 projects under `examples/` pass (`npm install && npm run examples`). The 5 `banned-*` examples pass by being rejected with the right diagnostic codes. `LEARNINGS.md` documents what broke before the toolchain existed and how that shaped the design. The compiled output is plain ESM; the worker example (`examples/23-cloudflare-worker`) exports the Cloudflare `{ fetch }` module shape and runs against real `Request`/`Response` objects, so `wrangler` can bundle it as-is.
+
TypeScript's early design decisions assumed humans would write and maintain it, so a lot of features exist to make a human's life easier. We valued speed over quality. With AI writing most of our code, we can flip that tradeoff and force the language to be declarative and verifiable, like Rust.
## What changes
View file
Addedexamples/01-hello/example.json+1−0
@@ -0,0 +1 @@
+{ "expect": "run", "output": ["Hello, tsus!"] }
View file
Addedexamples/01-hello/src/main.tsus+3−0
@@ -0,0 +1,3 @@
+const greet = (name: string): string => `Hello, ${name}!`;
+
+console.log(greet("tsus"));
View file
Addedexamples/02-option-basics/example.json+1−0
@@ -0,0 +1 @@
+{ "expect": "run", "output": ["2", "-1", "20"] }
View file
Addedexamples/02-option-basics/src/main.tsus+14−0
@@ -0,0 +1,14 @@
+import { Some, None, type Option } from "@tsus/runtime";
+
+const findFirst = (xs: number[], pred: (x: number) => boolean): Option<number> => {
+ for (const x of xs) {
+ if (pred(x)) {
+ return Some(x);
+ }
+ }
+ return None;
+};
+
+console.log(findFirst([1, 2, 3], (x) => x > 1).unwrapOr(-1));
+console.log(findFirst([1, 2, 3], (x) => x > 9).unwrapOr(-1));
+console.log(findFirst([1, 2, 3], (x) => x === 2).map((x) => x * 10).unwrapOr(0));
View file
Addedexamples/03-result-basics/example.json+1−0
@@ -0,0 +1 @@
+{ "expect": "run", "output": ["5", "-1", "4", "true"] }
View file
Addedexamples/03-result-basics/src/main.tsus+9−0
@@ -0,0 +1,9 @@
+import { Ok, Err, type Result } from "@tsus/runtime";
+
+const divide = (a: number, b: number): Result<number, string> =>
+ b === 0 ? Err("division by zero") : Ok(a / b);
+
+console.log(divide(10, 2).unwrapOr(-1));
+console.log(divide(10, 0).unwrapOr(-1));
+console.log(divide(9, 3).map((x) => x + 1).unwrapOr(-1));
+console.log(divide(1, 0).mapErr((e) => `error: ${e}`).isErr());
View file
Addedexamples/04-match-option/example.json+1−0
@@ -0,0 +1 @@
+{ "expect": "run", "output": ["Reach me at justin@example.com", "No email on file"] }
View file
Addedexamples/04-match-option/src/main.tsus+13−0
@@ -0,0 +1,13 @@
+import { Some, None, type Option } from "@tsus/runtime";
+
+const email: Option<string> = Some("justin@example.com");
+const missing: Option<string> = None;
+
+const describe = (opt: Option<string>): string =>
+ match (opt) {
+ Some(addr) => `Reach me at ${addr}`,
+ None => "No email on file",
+ };
+
+console.log(describe(email));
+console.log(describe(missing));
View file
Addedexamples/05-match-result/example.json+1−0
@@ -0,0 +1 @@
+{ "expect": "run", "output": ["listening on 8080", "bad port (not a number: banana)", "bad port (out of range: 99999)"] }
View file
Addedexamples/05-match-result/src/main.tsus+22−0
@@ -0,0 +1,22 @@
+import { Ok, Err, type Result } from "@tsus/runtime";
+
+const parsePort = (raw: string): Result<number, string> => {
+ const n = Number(raw);
+ if (Number.isNaN(n)) {
+ return Err(`not a number: ${raw}`);
+ }
+ if (n < 1 || n > 65535) {
+ return Err(`out of range: ${n}`);
+ }
+ return Ok(n);
+};
+
+const report = (raw: string): string =>
+ match (parsePort(raw)) {
+ Ok(port) => `listening on ${port}`,
+ Err(reason) => `bad port (${reason})`,
+ };
+
+console.log(report("8080"));
+console.log(report("banana"));
+console.log(report("99999"));
View file
Addedexamples/06-tagged-union/example.json+1−0
@@ -0,0 +1 @@
+{ "expect": "run", "output": ["13", "12", "0"] }
View file
Addedexamples/06-tagged-union/src/main.tsus+21−0
@@ -0,0 +1,21 @@
+import { tag, type Tagged } from "@tsus/runtime";
+
+type Shape =
+ | Tagged<"Circle", { r: number }>
+ | Tagged<"Rect", { w: number; h: number }>
+ | Tagged<"Point", {}>;
+
+const circle: Shape = tag("Circle", { r: 2 });
+const rect: Shape = tag("Rect", { w: 3, h: 4 });
+const point: Shape = tag("Point", {});
+
+const area = (s: Shape): number =>
+ match (s) {
+ Circle(c) => Math.round(Math.PI * c.r * c.r),
+ Rect(d) => d.w * d.h,
+ _ => 0,
+ };
+
+console.log(area(circle));
+console.log(area(rect));
+console.log(area(point));
View file
Addedexamples/07-banned-null/example.json+1−0
@@ -0,0 +1 @@
+{ "expect": "check-fail", "codes": ["TSUS002"] }
View file
Addedexamples/07-banned-null/src/main.tsus+3−0
@@ -0,0 +1,3 @@
+const legacy: string | null = null;
+
+console.log(legacy);
View file
Addedexamples/08-banned-class/example.json+1−0
@@ -0,0 +1 @@
+{ "expect": "check-fail", "codes": ["TSUS001"] }
View file
Addedexamples/08-banned-class/src/main.tsus+5−0
@@ -0,0 +1,5 @@
+class User {
+ constructor(public name: string) {}
+}
+
+console.log(new User("nope").name);
View file
Addedexamples/09-banned-var/example.json+1−0
@@ -0,0 +1 @@
+{ "expect": "check-fail", "codes": ["TSUS004"] }
View file
Addedexamples/09-banned-var/src/main.tsus+4−0
@@ -0,0 +1,4 @@
+var count = 0;
+count = count + 1;
+
+console.log(count);
View file
Addedexamples/10-banned-undefined/example.json+1−0
@@ -0,0 +1 @@
+{ "expect": "check-fail", "codes": ["TSUS003"] }
View file
Addedexamples/10-banned-undefined/src/main.tsus+3−0
@@ -0,0 +1,3 @@
+const maybe: string | undefined = undefined;
+
+console.log(typeof maybe);
View file
Addedexamples/11-banned-throw/example.json+1−0
@@ -0,0 +1 @@
+{ "expect": "check-fail", "codes": ["TSUS005"] }
View file
Addedexamples/11-banned-throw/src/main.tsus+5−0
@@ -0,0 +1,5 @@
+const explode = (msg: string): never => {
+ throw new Error(msg);
+};
+
+console.log(explode("boom"));
View file
Addedexamples/12-tsus-imports-tsus/example.json+1−0
@@ -0,0 +1 @@
+{ "expect": "run", "output": ["8", "-1"] }
View file
Addedexamples/12-tsus-imports-tsus/src/main.tsus+4−0
@@ -0,0 +1,4 @@
+import { safeSqrt, double } from "./math.tsus";
+
+console.log(safeSqrt(16).map(double).unwrapOr(-1));
+console.log(safeSqrt(-4).map(double).unwrapOr(-1));
View file
Addedexamples/12-tsus-imports-tsus/src/math.tsus+6−0
@@ -0,0 +1,6 @@
+import { Some, None, type Option } from "@tsus/runtime";
+
+export const safeSqrt = (x: number): Option<number> =>
+ x < 0 ? None : Some(Math.sqrt(x));
+
+export const double = (x: number): number => x * 2;
View file
Addedexamples/13-tsus-imports-ts/example.json+1−0
@@ -0,0 +1 @@
+{ "expect": "run", "output": ["1", "2", "justin", "nobody"] }
View file
Addedexamples/13-tsus-imports-ts/src/legacy.ts+19−0
@@ -0,0 +1,19 @@
+// Plain TypeScript: the strict rules do NOT apply here.
+// This file deliberately uses a class and null to prove that.
+class Counter {
+ private n: number = 0;
+ bump(): number {
+ this.n += 1;
+ return this.n;
+ }
+}
+
+const shared = new Counter();
+
+export function nextId(): number {
+ return shared.bump();
+}
+
+export function findUser(id: number): string | null {
+ return id === 1 ? "justin" : null;
+}
View file
Addedexamples/13-tsus-imports-ts/src/main.tsus+11−0
@@ -0,0 +1,11 @@
+import { fromNullable } from "@tsus/runtime/boundary";
+import { nextId, findUser } from "./legacy.ts";
+
+console.log(nextId());
+console.log(nextId());
+
+const user = fromNullable(findUser(1));
+const ghost = fromNullable(findUser(42));
+
+console.log(user.unwrapOr("nobody"));
+console.log(ghost.unwrapOr("nobody"));
View file
Addedexamples/14-ts-imports-tsus/example.json+1−0
@@ -0,0 +1 @@
+{ "expect": "run", "output": ["30", "-1", "true"] }
View file
Addedexamples/14-ts-imports-tsus/src/lib.tsus+9−0
@@ -0,0 +1,9 @@
+import { Ok, Err, type Result } from "@tsus/runtime";
+
+export const parseAge = (raw: string): Result<number, string> => {
+ const n = Number(raw);
+ if (Number.isNaN(n) || n < 0 || n > 150) {
+ return Err(`invalid age: ${raw}`);
+ }
+ return Ok(n);
+};
View file
Addedexamples/14-ts-imports-tsus/src/main.ts+9−0
@@ -0,0 +1,9 @@
+// Plain TypeScript entrypoint consuming a .tsus module.
+import { parseAge } from "./lib.tsus";
+
+const good = parseAge("30");
+const bad = parseAge("banana");
+
+console.log(good.unwrapOr(-1));
+console.log(bad.unwrapOr(-1));
+console.log(bad.isErr());
View file
Addedexamples/15-npm-dependency/example.json+1−0
@@ -0,0 +1 @@
+{ "expect": "run", "output": ["justin is 30", "rejected"] }
View file
Addedexamples/15-npm-dependency/src/main.tsus+26−0
@@ -0,0 +1,26 @@
+import { z } from "zod";
+import { Ok, Err, type Result } from "@tsus/runtime";
+
+const UserSchema = z.object({
+ name: z.string(),
+ age: z.number().int().min(0),
+});
+
+type User = z.infer<typeof UserSchema>;
+
+const parseUser = (input: unknown): Result<User, string> => {
+ const outcome = UserSchema.safeParse(input);
+ return outcome.success ? Ok(outcome.data) : Err(outcome.error.issues[0].message);
+};
+
+const good = parseUser({ name: "justin", age: 30 });
+const bad = parseUser({ name: "justin", age: "old" });
+
+console.log(match (good) {
+ Ok(u) => `${u.name} is ${u.age}`,
+ Err(e) => `rejected: ${e}`,
+});
+console.log(match (bad) {
+ Ok(u) => `${u.name} is ${u.age}`,
+ Err(e) => "rejected",
+});
View file
Addedexamples/16-boundary-fs/example.json+1−0
@@ -0,0 +1 @@
+{ "expect": "run", "output": ["hello from disk", "<read failed>"], "assets": ["src/data.txt"] }
View file
Addedexamples/16-boundary-fs/src/data.txt+1−0
@@ -0,0 +1 @@
+hello from disk
View file
Addedexamples/16-boundary-fs/src/main.tsus+13−0
@@ -0,0 +1,13 @@
+import { readFile } from "node:fs/promises";
+import { wrapAsync } from "@tsus/runtime/boundary";
+
+const safeRead = wrapAsync(readFile);
+
+const here = new URL("data.txt", import.meta.url).pathname;
+const nowhere = new URL("missing.txt", import.meta.url).pathname;
+
+const found = await safeRead(here, "utf8");
+const missing = await safeRead(nowhere, "utf8");
+
+console.log(found.map((text) => text.trim()).unwrapOr("<read failed>"));
+console.log(missing.map((text) => text.trim()).unwrapOr("<read failed>"));
View file
Addedexamples/17-nested-match/example.json+1−0
@@ -0,0 +1 @@
+{ "expect": "run", "output": ["retries=3", "retries=default", "broken: empty config"] }
View file
Addedexamples/17-nested-match/src/main.tsus+24−0
@@ -0,0 +1,24 @@
+import { Some, None, Ok, Err, type Option, type Result } from "@tsus/runtime";
+
+type Config = { retries: Option<number> };
+
+const loadConfig = (raw: string): Result<Config, string> => {
+ if (raw === "") {
+ return Err("empty config");
+ }
+ const n = Number(raw);
+ return Ok({ retries: Number.isNaN(n) ? None : Some(n) });
+};
+
+const describe = (raw: string): string =>
+ match (loadConfig(raw)) {
+ Ok(cfg) => match (cfg.retries) {
+ Some(n) => `retries=${n}`,
+ None => "retries=default",
+ },
+ Err(e) => `broken: ${e}`,
+ };
+
+console.log(describe("3"));
+console.log(describe("lots"));
+console.log(describe(""));
View file
Addedexamples/18-generics/example.json+1−0
@@ -0,0 +1 @@
+{ "expect": "run", "output": ["7", "-1", "x:1", "true"] }
View file
Addedexamples/18-generics/src/main.tsus+25−0
@@ -0,0 +1,25 @@
+import { Some, None, Ok, Err, type Option, type Result } from "@tsus/runtime";
+
+const firstOk = <T, E>(results: Result<T, E>[]): Option<T> => {
+ for (const r of results) {
+ if (r.isOk()) {
+ return r.ok();
+ }
+ }
+ return None;
+};
+
+const zip = <A, B>(a: Option<A>, b: Option<B>): Option<[A, B]> =>
+ a.andThen((va) => b.map((vb): [A, B] => [va, vb]));
+
+const attempts: Result<number, string>[] = [Err("cold"), Err("colder"), Ok(7), Ok(9)];
+
+console.log(firstOk(attempts).unwrapOr(-1));
+console.log(firstOk<number, string>([Err("a"), Err("b")]).unwrapOr(-1));
+
+const pair = zip(Some("x"), Some(1));
+console.log(match (pair) {
+ Some(p) => `${p[0]}:${p[1]}`,
+ None => "no pair",
+});
+console.log(zip(Some("x"), None).isNone());
View file
Addedexamples/19-async-result/example.json+1−0
@@ -0,0 +1 @@
+{ "expect": "run", "output": ["PAYLOAD", "failed: 404 /nope"] }
View file
Addedexamples/19-async-result/src/main.tsus+26−0
@@ -0,0 +1,26 @@
+import { Ok, Err, type Result } from "@tsus/runtime";
+
+const fakeFetch = async (path: string): Promise<Result<string, string>> => {
+ await Promise.resolve();
+ return path === "/ok" ? Ok("payload") : Err(`404 ${path}`);
+};
+
+const enrich = async (body: string): Promise<string> => {
+ await Promise.resolve();
+ return body.toUpperCase();
+};
+
+const good = await fakeFetch("/ok");
+const bad = await fakeFetch("/nope");
+
+const goodMsg = await match (good) {
+ Ok(body) => await enrich(body),
+ Err(e) => `failed: ${e}`,
+};
+const badMsg = await match (bad) {
+ Ok(body) => await enrich(body),
+ Err(e) => `failed: ${e}`,
+};
+
+console.log(goodMsg);
+console.log(badMsg);
View file
Addedexamples/20-deep-chain/example.json+1−0
@@ -0,0 +1 @@
+{ "expect": "run", "output": ["chain says 5"] }
View file
Addedexamples/20-deep-chain/src/b.tsus+4−0
@@ -0,0 +1,4 @@
+import { squared } from "./c.tsus";
+import { Some, type Option } from "@tsus/runtime";
+
+export const wrapped = (): Option<number> => Some(squared() + 1);
View file
Addedexamples/20-deep-chain/src/c.tsus+3−0
@@ -0,0 +1,3 @@
+import { base } from "./d.tsus";
+
+export const squared = (): number => base() * base();
View file
Addedexamples/20-deep-chain/src/d.tsus+1−0
@@ -0,0 +1 @@
+export const base = (): number => 2;
View file
Addedexamples/20-deep-chain/src/main.tsus+6−0
@@ -0,0 +1,6 @@
+import { wrapped } from "./b.tsus";
+
+console.log(match (wrapped()) {
+ Some(n) => `chain says ${n}`,
+ None => "chain broke",
+});
View file
Addedexamples/21-circular-imports/example.json+1−0
@@ -0,0 +1 @@
+{ "expect": "run", "output": ["true", "true", "false"] }
View file
Addedexamples/21-circular-imports/src/even.tsus+3−0
@@ -0,0 +1,3 @@
+import { isOdd } from "./odd.tsus";
+
+export const isEven = (n: number): boolean => (n === 0 ? true : isOdd(n - 1));
View file
Addedexamples/21-circular-imports/src/main.tsus+6−0
@@ -0,0 +1,6 @@
+import { isEven } from "./even.tsus";
+import { isOdd } from "./odd.tsus";
+
+console.log(isEven(10));
+console.log(isOdd(7));
+console.log(isEven(3));
View file
Addedexamples/21-circular-imports/src/odd.tsus+3−0
@@ -0,0 +1,3 @@
+import { isEven } from "./even.tsus";
+
+export const isOdd = (n: number): boolean => (n === 0 ? false : isEven(n - 1));
View file
Addedexamples/22-mixed-project/example.json+1−0
@@ -0,0 +1 @@
+{ "expect": "run", "output": ["ada (36)", "grace (45)", "not found"] }
View file
Addedexamples/22-mixed-project/src/main.tsus+14−0
@@ -0,0 +1,14 @@
+import { loadRow } from "./repo.tsus";
+
+const show = (id: number): string =>
+ match (loadRow(id)) {
+ Some(validated) => match (validated) {
+ Ok(row) => `${row.name} (${row.age})`,
+ Err(e) => `bad data: ${e}`,
+ },
+ None => "not found",
+ };
+
+console.log(show(1));
+console.log(show(2));
+console.log(show(99));
View file
Addedexamples/22-mixed-project/src/repo.tsus+7−0
@@ -0,0 +1,7 @@
+import { fromNullable } from "@tsus/runtime/boundary";
+import { type Option, type Result } from "@tsus/runtime";
+import { getRow } from "./store.ts";
+import { validateRow, type Row } from "./validate.tsus";
+
+export const loadRow = (id: number): Option<Result<Row, string>> =>
+ fromNullable(getRow(id)).map(validateRow);
View file
Addedexamples/22-mixed-project/src/store.ts+8−0
@@ -0,0 +1,8 @@
+// Plain TS in-memory store. Uses undefined internally: allowed here, not in .tsus.
+const rows = new Map<number, { name: string; age: number }>();
+rows.set(1, { name: "ada", age: 36 });
+rows.set(2, { name: "grace", age: 45 });
+
+export function getRow(id: number): { name: string; age: number } | undefined {
+ return rows.get(id);
+}
View file
Addedexamples/22-mixed-project/src/validate.tsus+11−0
@@ -0,0 +1,11 @@
+import { z } from "zod";
+import { Ok, Err, type Result } from "@tsus/runtime";
+
+const RowSchema = z.object({ name: z.string().min(1), age: z.number().min(18) });
+
+export type Row = z.infer<typeof RowSchema>;
+
+export const validateRow = (input: unknown): Result<Row, string> => {
+ const outcome = RowSchema.safeParse(input);
+ return outcome.success ? Ok(outcome.data) : Err("invalid row");
+};
View file
Addedexamples/23-cloudflare-worker/example.json+9−0
@@ -0,0 +1,9 @@
+{
+ "expect": "worker",
+ "entry": "worker.js",
+ "requests": [
+ { "path": "/", "status": 200, "body": "home sweet home" },
+ { "path": "/health", "status": 200, "body": "ok" },
+ { "path": "/nope", "status": 404, "body": "not found" }
+ ]
+}
View file
Addedexamples/23-cloudflare-worker/src/router.tsus

Not shown: this page diffs the first 60 files. View the file or the raw file.

View file
Addedexamples/23-cloudflare-worker/src/worker.tsus

Not shown: this page diffs the first 60 files. View the file or the raw file.

View file
Addedexamples/24-hostile-syntax/example.json

Not shown: this page diffs the first 60 files. View the file or the raw file.

View file
Addedexamples/24-hostile-syntax/src/main.tsus

Not shown: this page diffs the first 60 files. View the file or the raw file.

View file
Addedexamples/25-shadowed-match/example.json

Not shown: this page diffs the first 60 files. View the file or the raw file.

View file
Addedexamples/25-shadowed-match/src/main.tsus

Not shown: this page diffs the first 60 files. View the file or the raw file.

View file
Addedexamples/26-json-boundary/example.json

Not shown: this page diffs the first 60 files. View the file or the raw file.

View file
Addedexamples/26-json-boundary/src/main.tsus

Not shown: this page diffs the first 60 files. View the file or the raw file.

View file
Addedpackage-lock.json

Not shown: this page diffs the first 60 files. View the file or the raw file.

View file
Addedpackage.json

Not shown: this page diffs the first 60 files. View the file or the raw file.

View file
Addedpackages/cli/bin/tsus.js

Not shown: this page diffs the first 60 files. View the file or the raw file.

View file
Addedpackages/cli/package.json

Not shown: this page diffs the first 60 files. View the file or the raw file.

View file
Addedpackages/cli/src/build.js

Not shown: this page diffs the first 60 files. View the file or the raw file.

View file
Addedpackages/cli/src/check.js

Not shown: this page diffs the first 60 files. View the file or the raw file.

View file
Addedpackages/cli/src/preprocess.js

Not shown: this page diffs the first 60 files. View the file or the raw file.

View file
Addedpackages/runtime/boundary.d.ts

Not shown: this page diffs the first 60 files. View the file or the raw file.

View file
Addedpackages/runtime/boundary.js

Not shown: this page diffs the first 60 files. View the file or the raw file.

View file
Addedpackages/runtime/index.d.ts

Not shown: this page diffs the first 60 files. View the file or the raw file.

View file
Addedpackages/runtime/index.js

Not shown: this page diffs the first 60 files. View the file or the raw file.

View file
Addedpackages/runtime/package.json

Not shown: this page diffs the first 60 files. View the file or the raw file.

View file
Addedscripts/run-examples.mjs

Not shown: this page diffs the first 60 files. View the file or the raw file.

View file

Checks

No checks reported for this commit.

Runs

No runs for this commit.