Files
scriptc/tests/diagnostics/record-width.ts
T
Malte UblandClaude Opus 5.5 acb0a4c9ed Adapt method fields across instantiations with checked conversions
A record width copy such as Provider<"beta"> into Provider<Api> failed
on method fields whose parameter record depends on the instantiation:
TypeScript admits them by method bivariance, but the adapter only took
mechanical parameter conversions. Inside a record's field plan, such a
field now takes a checked adapter: parameters convert with the record
width plans, and union arms without a home throw the catchable
TypeError. Top-level function conversions and every pair that planned
before are unchanged.

The safe direction lifts too: record-width's method field whose parameter
record widens now plans, so its SC2002 refusal gives way to the later
honest SC1090 (a function value has no inspect lowering).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-29 05:37:46 -07:00

42 lines
1.6 KiB
TypeScript

// SC2002: the RESIDUE of the width-copy family — record shapes that
// neither match exactly nor width-coerce. Each fence names the first
// blocking rule. (The width copies themselves — field-subset narrowing,
// hybrid captures, per-field lifts — are differential corpus.)
// An index-signature source cannot complete an absent optional field:
// the overflow could hold the key at runtime, and a completed undefined
// would drop that value.
type Loose = { [k: string]: number };
type PickedA = { a?: number };
const loose: Loose = { a: 1 };
const picked: PickedA = loose;
console.log(picked);
// A method field whose parameter shapes relate only BIVARIANTLY: the field
// lifts as a checked adapter, so only the console.log below refuses (a
// function value has no inspect lowering).
type SrcM = { m(x: { a: number }): void; extra: number };
type DstM = { m(x: { a: number; b: number }): void };
const srcM: SrcM = {
m: (x) => {
console.log(x.a);
},
extra: 1,
};
const dstM: DstM = srcM;
console.log(dstM);
// A source field that cannot enter the target's value slot (a Map has no
// dyn conversion into an 'unknown' signature slot).
type WithMap = { m: Map<string, number>; n: number };
const withMap: WithMap = { m: new Map(), n: 1 };
const bag: Record<string, unknown> = withMap;
console.log(bag);
// A declared target field that cannot take a runtime key collision from
// the signature slot (its type is narrower than the slot's).
type Odd = { a?: boolean; [k: string]: boolean | string | undefined };
const plain = { b: "hi" };
const odd: Odd = plain;
console.log(odd);