Files
scriptc/tests/corpus/record-source-keys-index-values.ts
T
Chris Tate eccb061a12 Find kept record sources through index signatures and record fields
The capture into an index-signature record consults a record's kept source
view only when some checked conversion in the program can keep one. That
scan accepted a closed record target and looked through union arms,
promises and arrays, but stopped at an index-signature record or a record
whose fields hold closed records. A conversion into
`Record<string, { x: string }>` keeps each value's undeclared keys, so a
program whose only such conversion had that shape dropped them in the
capture. The scan now looks through record fields and index-signature
values too, memoizing records by shape as it does unions.
2026-10-29 08:51:27 -05:00

20 lines
887 B
TypeScript

// A checked conversion into an index-signature record whose values are
// closed records keeps each value's undeclared source keys, so converting
// such a value into an index-signature record later copies them. This
// program has no checked conversion into a closed record itself, so the
// capture learns that kept sources are possible from the index signature's
// values. Node prints "extra,x".
type H = Record<string, string | null>;
interface T {
transformHeaders?: (h: H) => H | Promise<H>;
}
const parsed: Record<string, { x: string }> = JSON.parse('{"p":{"x":"1","extra":"e"}}');
async function viaArrow<O extends T>(o: O | undefined): Promise<H> {
let h: H = { accept: "a" };
if (o?.transformHeaders) h = await o.transformHeaders(h);
return h;
}
viaArrow({ transformHeaders: async (h) => parsed["p"]! }).then((r) =>
console.log(Object.keys(r).sort().join(",")),
);