mirror of
https://github.com/vercel-labs/scriptc.git
synced 2026-11-08 08:44:37 +08:00
- `Promise.all([p, q, r])` over different promise types (the tuple overload) lowers in TypeScript through one interned async helper that awaits the runtime combinator over the same promises (widened to Promise<void>, a type-only view), so settlement order and rejection handling are Node's, then reads each fulfilled entry into the tuple. Entries keep their identity; plain scalar entries pass through, and a checked-dynamic entry joins through the promise it holds. - The ambient `Boolean` as an `every`/`some`/`find`/`findIndex`/`map` callback in TypeScript is the interned one-parameter truthiness closure `filter(Boolean)` already used, and `filter(Boolean)` accepts checked-dynamic elements (runtime truthiness). - `E[key]` with a runtime string key answers the named member's constant, or undefined for a name no member carries; a numeric member's reverse key and inherited Object.prototype names throw a named runtime refusal. - Regex-argument `replace`/`replaceAll`/`split`/`search`/`matchAll` on a `string | undefined` receiver take the string lowering and throw Node's "Cannot read properties of undefined (reading 'replace')". Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
35 lines
1.4 KiB
TypeScript
35 lines
1.4 KiB
TypeScript
// The ambient Boolean as an array callback beyond filter: every, some,
|
|
// find, findIndex and map call it with the element first, so each answers
|
|
// ToBoolean of the element (later arguments are ignored). filter(Boolean)
|
|
// also covers elements the checker types `any`.
|
|
const flags = [true, true, false];
|
|
console.log(flags.every(Boolean), flags.some(Boolean), flags.slice(0, 2).every(Boolean));
|
|
const counts = [0, 3, 0, 7];
|
|
console.log(counts.find(Boolean), counts.findIndex(Boolean), counts.map(Boolean).join(","));
|
|
const names = ["", "a", "", "b"];
|
|
console.log(names.some(Boolean), names.every(Boolean), names.findIndex(Boolean));
|
|
const maybe: (string | undefined)[] = [undefined, "x", undefined];
|
|
console.log(maybe.some(Boolean), maybe.find(Boolean), maybe.map(Boolean).join(","));
|
|
|
|
// Elements the checker types `any` keep their runtime kind; filter(Boolean)
|
|
// answers their runtime truthiness.
|
|
function untyped(value: unknown): any {
|
|
return value;
|
|
}
|
|
const configFiles = [untyped("app.json"), untyped(""), untyped(undefined), untyped("site.json")].filter(
|
|
Boolean,
|
|
) as string[];
|
|
console.log(configFiles.length, configFiles.join(","));
|
|
|
|
async function check(n: number): Promise<boolean> {
|
|
return n % 2 === 1;
|
|
}
|
|
|
|
async function main(): Promise<void> {
|
|
const all = (await Promise.all([1, 3, 5].map(async (n) => check(n)))).every(Boolean);
|
|
const any = (await Promise.all([2, 4].map(async (n) => check(n)))).some(Boolean);
|
|
console.log(all, any);
|
|
}
|
|
|
|
main();
|