Files
scriptc/tests/corpus/optional-call-native-method.ts
T
Malte UblandClaude Opus 5.5 def4626396 Lower optional calls of always-present library methods as plain calls
`h.getSetCookie?.()` on a native Headers handle failed with SC2020: the
optional-chain lowering first reads the callee as a value, and native
handle methods have no first-class value. A required standard-library
method on a never-nullish receiver always exists, so `?.()` is the plain
call.

When that method read has no lowering, the optional chain now re-dispatches
the plain call. Reads that lower keep the existing path unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-18 15:16:15 -07:00

29 lines
885 B
TypeScript

// An optional call of a standard-library method that always exists on a
// never-nullish receiver is the plain call (`getSetCookie?.()` is always
// present on Node 24).
const h = new Headers();
h.append("set-cookie", "a=1");
h.append("set-cookie", "b=2");
const cookies = h.getSetCookie?.() ?? [];
console.log(JSON.stringify(cookies));
console.log(h.get?.("set-cookie"));
console.log(h.has?.("x-missing"));
h.set?.("x-set", "1");
h.forEach?.((value, name) => console.log(`${name}=${value}`));
// The receiver evaluates once; arguments evaluate after it.
let reads = 0;
function headers(): Headers {
reads++;
return h;
}
function arg(): string {
console.log("argument after receiver", reads);
return "x-set";
}
console.log(headers().get?.(arg()), reads);
// Strings and arrays: the same rule.
const s = "Hello";
console.log(s.toUpperCase?.(), [3, 1, 2].sort?.().join(","));