Files
scriptc/tests/corpus/3089-array-find-narrowing.ts
Chris Tate ffe5e27563 feat(compiler): run the IR validator natively (#493)
- Compile the production validator and exercise native IR construction and validation on C and LLVM.
- Preserve Error causes, wider callbacks, and discriminated JSON layouts in static programs.
- Version the IR and runtime ABI and cover the new compiler paths with differential and sanitizer tests.
2026-09-27 13:11:56 -05:00

54 lines
2.1 KiB
TypeScript

export {};
type Leaf = { kind: "leaf"; value: number };
type Text = { kind: "text"; text: string };
type Empty = { kind: "empty" };
type Node = Leaf | Text | Empty;
const nodes: Node[] = [{ kind: "empty" }, { kind: "leaf", value: 3 }, { kind: "text", text: "middle" }, { kind: "leaf", value: 9 }];
const first = nodes.find((node) => node.kind === "leaf");
const last = nodes.findLast((node) => node.kind === "leaf");
console.log("leaf", first?.value, last?.value);
console.log("text", nodes.find((node) => node.kind === "text")?.text);
console.log("miss", nodes.slice(0, 1).find((node) => node.kind === "leaf") === undefined);
const mixed: (string | number | undefined)[] = [undefined, "first", 3, "last"];
console.log("scalars", mixed.find((value) => typeof value === "string"), mixed.findLast((value) => typeof value === "string"));
const present = mixed.find((value) => value !== undefined);
console.log("subunion", typeof present, present);
const empty: (string | number)[] = [];
console.log("empty", empty.find((value) => typeof value === "number"));
const sparse: string[] = [, "two", "three"] as string[];
let visits = "";
const found = sparse.find((value, index) => {
visits += index + ":" + String(value) + ";";
return typeof value === "string";
});
console.log("holes", visits, found);
const mutable: Node[] = [{ kind: "leaf", value: 42 }];
const saved = mutable.find((node) => {
mutable[0] = { kind: "empty" };
return node.kind === "leaf";
});
console.log("snapshot", saved?.value, mutable[0]!.kind);
let reverse = "";
const reverseFound = mixed.findLast((value, index) => {
reverse += index + ";";
return typeof value === "number";
});
console.log("reverse", reverse, reverseFound);
function acceptsWide(value: Node | undefined): boolean { return value?.kind === "text"; }
const wide = nodes.find(acceptsWide);
if (wide?.kind === "text") console.log("callback value", wide.text);
try {
nodes.find((node) => {
if (node.kind === "leaf") throw new Error("predicate");
return node.kind === "text";
});
} catch (error) {
if (error instanceof Error) console.log("unwind", error.message);
}