ReviewOS

home-lang/home

fix(ts): preserve FlatArray element types for readonly union arrays

#973
Open glennmichael123 opened this 12 days ago · 9 comments
12 days ago

Related #960, #846, #66.

TypeScript 5.7.3 control (--noEmit --strict --target es2023 --skipLibCheck):

declare const values: readonly (number[] | string[])[];
const flattened = values.flat();
const numbers: number[] = flattened;
const booleans: boolean[] = flattened;

TypeScript reports TS2322 for both assignments because flattened is (string | number)[]. Fresh Home home-tsc exits 0 with no diagnostics for the same source. Both runs were supervised under the 3,840 MB guard.

packages/ts_checker/src/lib.zig currently sets the synthetic flat() result to any[] whenever the element type is a union. Its own comment says this is a fallback for missing distributive flattening. This loses type information and accepts impossible assignments. The same implementation models only default depth 1, so explicit depth and nested arrays need a matched oracle too.

Acceptance: reproduce this as a repository TypeScript 5.7.3 exact-stream fixture for readonly and mutable receivers, then implement the underlying FlatArray result semantics for union elements and supported depth forms without a permissive any[] fallback. Preserve array callback/member behavior, run focused and full checker, Program, LSP, and the expanded CLI stream audit under the memory guard. Keep #960 and #66 updated.

12 days ago

The matched #973 oracle exposed a second, independent import-side error, now tracked in #974: JSDoc ReadonlyArray<number[] | string[]> was projected as an array of number[] | string because a postfix [] was stripped before the top-level union split. Both root fixes and a combined checked-JavaScript CLI fixture are in the working checkout; guarded verification is in progress before any commit.

12 days ago

Progress (not closed): the working checkout now distributes flat()'s default-depth result over number[] | string[] rather than widening it to any[]. A focused checker unit test covers (number | string)[], and a combined admitted-JavaScript fixture checks readonly and mutable receivers in both checkJs modes. The fixture also exposed #974 (JSDoc union precedence), whose parser fix is in the checkout, and #975 (TypeScript 5.7.3 union diagnostic order depends on skipLibCheck).

A matched TS 5.7.3 control confirms four intentional TS2322 errors in the fixture. Home's last built CLI has the correct semantic types and four errors, but its nested union explanation is one line short and the imported union order differs. A focused checker patch for the missing child is added but not yet rebuilt; host memory is below the agreed 40% Zig build admission gate. Explicit flat(depth) forms remain open in this issue. No commit or parity claim yet.

12 days ago

Pushed 801194f82 to origin/main as Glenn, without coauthors. It fixes the nested TS2322 explanation when every member of an array element union fails assignment; the focused regression and full checker suite passed under the 3,840 MB guard. The rebuilt CLI now matches TypeScript's nested chain in both checkJs modes.

The default-depth flat() result and #974 JSDoc precedence changes remain uncommitted pending full Program/LSP verification and the separate #975 union display-order gap. An explicit flat(2) control still shows a real semantic gap: TypeScript returns number[] from readonly number[][][], while Home returns number[][]. #973 stays open.

12 days ago

Additional matched semantic control found a flaw in the uncommitted flat() implementation, so it must not be pushed yet:

interface ArrayLikeOnly { length: number; [index: number]: number }
declare const values: readonly ArrayLikeOnly[];
const numeric: number[] = values.flat();
const original: ArrayLikeOnly[] = values.flat();

TypeScript 5.7.3 emits TS2322 only for numeric: FlatArray tests Arr extends ReadonlyArray<infer InnerArr>, and this indexed object is not a ReadonlyArray. The current Home binary emits TS2322 only for original, because packages/ts_checker/src/lib.zig treats any numeric index as proof that the element is flattenable. This is a real semantic inversion, separate from #975's display ordering and the explicit flat(2) depth gap. The fix needs an array/readonly-array relation that distinguishes actual arrays from indexed objects; an objectNumberIndex or [number] + length shape check is insufficient.

12 days ago

Pushed 4be9ed980 and b214ad08c to main. Default-depth flat() now distributes over array unions using array/tuple provenance, and preserves indexed objects such as ArrayLikeOnly instead of flattening them. Imported Program array types retain their origin, and incompatible union-array assignments include the element explanation. The new flat.ts oracle has the same five diagnostic sites and explanations as TypeScript 5.7.3 in both checkJs modes.

Guarded full checker, Program, and LSP suites passed. The expanded admitted-JavaScript exact-stream audit is 153/155: its two mismatches are only the imported union display order in the new fixture, tracked by #975. This issue remains open for explicit depth (flat(2)) and fuller ReadonlyArray conditional-type semantics.

12 days ago

Matched TypeScript 5.7.3 control for the remaining FlatArray semantics:

interface Arrayish extends ReadonlyArray<number> { tag: string }
declare const nested: readonly number[][][];
const depth0: number[][][] = nested.flat(0);
const depth1: number[][] = nested.flat();
const depth2: number[] = nested.flat(2);
const wrong2: string[] = nested.flat(2);
declare const structural: readonly Arrayish[];
const structuralFlat: number[] = structural.flat();

Under --noEmit --strict --skipLibCheck --target es2023, TypeScript reports only the intentional TS2322 on wrong2 (line 6). Fresh Home reports extra TS2322s on depth0, depth2, and structuralFlat, and describes wrong2 as number[][] rather than number[]. This confirms that the remaining work includes depth-sensitive FlatArray evaluation and the extends ReadonlyArray relation for interfaces, beyond the array-origin check shipped in #973's default-depth batch. No patch for these cases is committed yet.

12 days ago

The #960 structural control narrows the FlatArray blocker: ReadonlyIndexed with only readonly length and [index: number] is accepted by Home as ReadonlyArray<number>, but TypeScript 5.7.3 rejects that assignment with TS2740 for missing array methods. In contrast, Arrayish extends ReadonlyArray<number> is accepted by TypeScript and is flattened by flat(), while Home currently leaves Arrayish intact. See the #960 control and exact diagnostics. The fix needs a complete ReadonlyArray relation before using it to decide whether FlatArray<Arr, D> unwraps a user-defined element type.

10 days ago

Pushed 7f36cc3fe and 2afb48bfc to main. Explicit flat(depth) now evaluates FlatArray<A, D> instead of reusing the default-depth element.

FlatArray<Arr, Depth> unwraps one ReadonlyArray level per depth step, counting the receiver itself, stops as soon as the operand is no longer a readonly array, and distributes over a union with each constituent spending its own remaining budget; -1 is the done key and returns the operand whole. The checker now shares one depth-driven evaluation for the default and the explicit forms, selects it only for its own synthetic flat member (recognized through a lib-cache record, since object types are not interned by structure), reads the depth from the argument's literal type or else its syntax — checkExpression types every bare numeric literal as number, so an overload set keyed on literal depth types could not select here — and re-applies the undefined widening of an optional-chained call.

Matched TypeScript 5.7.3 controls (--noEmit --strict --skipLibCheck --target es2023) now agree on the whole reported gap:

  • readonly number[][][]: flat(0) → number[][][], flat(1) → number[][], flat(2) → number[], flat(9) saturates at number[].
  • interface Arrayish extends ReadonlyArray<number>: flat(0) keeps Arrayish[] and flat(2) reaches number[], while the numeric-index ArrayLikeOnly is preserved at every depth.
  • A readonly tuple keeps its element union at depth 0 and flattens it at depth 1; 2 as const counts as depth 2; chained calls compose; imported checked-JavaScript arrays follow the same rule in both checkJs modes; maybe?.flat(2) keeps number[] | undefined.

A depth above 21 or a non-integral one flattens every remaining level, which is what TypeScript's 22-element decrement tuple does once the index leaves its range.

New oracle: packages/ts_cli/testdata/issue846-readonly-array/flat-depth.ts with tsconfig-flat-depth.json and its no-checkJs control — 17 accepted assignments pinning each depth plus the two intentional TS2322 diagnostics, byte-identical to TypeScript 5.7.3 in both modes.

Gates under the 3,840 MB guard: direct checker 4,757/4,757, Program 473/473, and the expanded exact-stream audit at 165 streams, 163 exact matches. The two mismatches are the pre-existing imported-union display order in flat.ts(5,7) tracked by #975 — the same two as before this batch. The audit itself could not start on this host because it shelled out to rg, which is not installed; accecc813 enumerates its fixtures with Node instead. The guarded LSP step could not run: the ts_lsp debug compile now measures 3,961 MB at the committed 8 shards and 4,006 MB at 16, so it exceeds the ceiling regardless of sharding (exit 125); that is an artifact-compile footprint, in the same class as #855, and sharding is not the lever.

Two differences remain, so this issue stays open:

  • A depth that is not statically known (nested.flat(depthVar)) still yields the default-depth result where TypeScript keeps an unresolved FlatArray<number[][], 0 | 1 | -1 | ... | 20>. Both engines report the assignment, with different text.
  • (readonly number[])[] renders as readonly number[][]. That is a pre-existing array-display gap unrelated to flat — declare const x: (readonly number[])[] shows it on its own — and it is what makes flat(-1)'s text differ.
7 days ago

Verified fixed on current main; this looks closeable.

The any[] fallback for union elements is gone. The issue's exact repro, run with --noEmit --strict --target es2023 --skipLibCheck under the 3,840 MB guard, is now byte-identical between tsc 5.7.3 and Home:

declare const values: readonly (number[] | string[])[];
const flattened = values.flat();
const numbers: number[] = flattened;
const booleans: boolean[] = flattened;
v.ts(3,7): error TS2322: Type '(string | number)[]' is not assignable to type 'number[]'.
  Type 'string | number' is not assignable to type 'number'.
    Type 'string' is not assignable to type 'number'.
v.ts(4,7): error TS2322: Type '(string | number)[]' is not assignable to type 'boolean[]'.
  Type 'string | number' is not assignable to type 'boolean'.
    Type 'string' is not assignable to type 'boolean'.

flattened is (string | number)[], both impossible assignments are reported, and the chains match. The work landed across 801194f82, 4be9ed980, b214ad08c, 7f36cc3fe and 2afb48bfc, the last two covering explicit depths with their own pinned oracle — so the "explicit depth and nested arrays need a matched oracle too" part is covered as well.

Sign in to comment on this issue.