mirror of
https://github.com/vercel-labs/scriptc.git
synced 2026-11-08 08:44:37 +08:00
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.
20 lines
887 B
TypeScript
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(",")),
|
|
);
|