mirror of
https://github.com/vercel-labs/scriptc.git
synced 2026-10-02 08:35:07 +08:00
- Share frontend loading, package selection, contracts, and FFI parsing between Node and native clients. - Preserve collection layouts and optional method returns while lowering long nullish chains without recursive stack growth. - Rebuild the frontend from TypeScript through native lowering and C emission with matching output.
37 lines
1.3 KiB
TypeScript
37 lines
1.3 KiB
TypeScript
type Priority = readonly number[] | { rank: readonly number[] };
|
|
|
|
function describe(priority: Priority): string {
|
|
return Array.isArray(priority) ? priority.join(":") : (priority as { rank: readonly number[] }).rank.join(":");
|
|
}
|
|
|
|
function withPriority<T>(priority: Priority, callback: () => T): T {
|
|
console.log(describe(priority));
|
|
return callback();
|
|
}
|
|
|
|
["first", "second"].forEach((name, index) => {
|
|
const priority = [2, index] as const;
|
|
console.log(withPriority(priority, () => name));
|
|
});
|
|
const direct: Priority = [3, 4] as const;
|
|
console.log(describe(direct));
|
|
console.log(describe({ rank: [5, 6] }));
|
|
|
|
type Mixed = readonly (number | string)[] | { label: string };
|
|
function showMixed(value: Mixed): void {
|
|
console.log(Array.isArray(value) ? value.join(",") : (value as { label: string }).label);
|
|
}
|
|
const mixed = [7, "eight"] as const;
|
|
showMixed(mixed);
|
|
showMixed({ label: "record" });
|
|
|
|
type Rows = readonly { value: number }[] | { count: number } | undefined;
|
|
function showRows(rows: Rows): void {
|
|
if (rows === undefined) { console.log("undefined"); return; }
|
|
console.log(Array.isArray(rows) ? rows.map((row): number => row.value).join(",") : (rows as { count: number }).count);
|
|
}
|
|
const rows = [{ value: 9 }, { value: 10 }] as const;
|
|
showRows(rows);
|
|
showRows({ count: 11 });
|
|
showRows(undefined);
|