Skip to content

feat(attest): add normalized type snapshots - #1714

Open
MDerman wants to merge 1 commit into
arktypeio:mainfrom
MDerman:feat/attest-normalized-snap
Open

MDerman wants to merge 1 commit into
arktypeio:mainfrom
MDerman:feat/attest-normalized-snap

Conversation

@MDerman

@MDerman MDerman commented Oct 26, 2026

Copy link
Copy Markdown

Type snapshots can fail after a checker upgrade when only property or union order changes.

Adds attest(value).type.toString.normalizedSnap(), which sorts properties and union members recursively before saving or comparing the snapshot. It uses Prettier's bundled TypeScript parser, so the same implementation works with TypeScript 6, tsgo and tsrs without a new dependency. Tuple elements, parameters, intersections and overloads keep their order.

Supports inline generation, snapshot updates and external snapshot files. Includes usage docs and integration coverage for nested types, literal text, order-sensitive constructs and real type changes.

Fixes #1270.

Validation:

  • Lint and package/docs builds passed.
  • pnpm testRepoWithBenches passed, including repository typechecking, tests, benchmarks and the TypeScript compatibility matrix.
  • Seven focused normalization and snapshot persistence tests passed, including generation through the compiled package.

Implemented with GPT-6 Astra through Codex in T3 Code.

@github-project-automation github-project-automation Bot moved this to To do in arktypeio Oct 26, 2026
@MDerman
MDerman marked this pull request as ready for review October 28, 2026 18:27
@ssalbdivad

Copy link
Copy Markdown
Member

really clean approach — using prettier's TS parser keeps it checker-agnostic with no new deps, and the tests/docs look great.

needs a quick rebase onto main after 3.0 landed (assertions.mdx / llms.txt moved around, and externalSnapshots.test.ts now needs attestInternal(..., { cfg: { failOnMissingSnapshots: false } }) for the new .normalizedSnap.toFile("normalized") call so it passes with CI=true).

two tiny nits while you're in there:

  1. in normalizedType.ts, trimming trailing [;,] off each member's sort key inside TSTypeLiteral prevents union order from depending on whether a checker emits trailing semicolons on the last property ({ a: 1 b: 2; } vs { a: 1; b: 2 }).
  2. in chainableAssertions.ts, toFile's MissingSnapshotError hardcodes .snap.toFile — might as well use this.ctx.lastSnapName ?? "snap" like inline does.

once rebased this looks good to merge!

@KilianTheMagnificent

Copy link
Copy Markdown

Tested c2f6ae6a92 (tsrs 65/65, tsgo 7.1-beta 65/65, tsc@6.0.3 63/63; base edf9c17316 63/63); two notes beyond the [;,] trim and toFile error noted above:

  • TS 6 vs TS 7 symbol-member drift (normalizedType.ts:64-66): tsc@6.0.3 puts computed symbol members last while tsgo/tsrs keep declaration slots, so slot-pinning diverges on the same type literal (21/55 cases): { z: 1; [Symbol.toStringTag]: "value"; a: 2 } → { a: 2; z: 1; [Symbol.toStringTag]: "value" } (TS 6) vs { a: 2; [Symbol.toStringTag]: "value"; z: 1 } (tsgo/tsrs). Same for unique symbols, [Symbol.iterator](), overloads, get/set, Partial/Pick/Omit, and normalizedSnap.test.ts:31-38 if [Symbol.toStringTag] moves earlier. Appending computed properties/methods in relative order cut 21/55 → 0/55 (65/65 tsrs/tsgo, 63/63 TS 6), unless keeping checker slots is intended.
  • Stale lastSnapName (chainableAssertions.ts:124,180): Reading and clearing this.ctx.lastSnapName once in get snap() covers inline, toFile, and .unwrap(), keeping a reused attest(v) handle's later a.type.toString.snap() from failing as normalizedSnap (pre-existing with completions at :247).

AI-assisted verification and drafting on behalf of @KilianTheMagnificent; all checks above were executed locally.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: To do

Development

Successfully merging this pull request may close these issues.

attest: add option to normalize property/union order when serializing types

3 participants