Skip to content

Strict types for src/_lib/eleventy/pdf.js#1327

Open
stefan-burke wants to merge 5 commits into
mainfrom
claude/cool-babbage-OwSbn
Open

Strict types for src/_lib/eleventy/pdf.js#1327
stefan-burke wants to merge 5 commits into
mainfrom
claude/cool-babbage-OwSbn

Conversation

@stefan-burke
Copy link
Copy Markdown
Member

Summary

Adds JSDoc type annotations to src/_lib/eleventy/pdf.js so it passes
tsc --strict. The ratchet baseline drops from 395 → 374 errors and
pdf.js joins STRICT_CLEAN_FILES.

Changes

  • src/_lib/eleventy/pdf.js — Strict types via JSDoc, no inline
    @type annotations inside function bodies, no any. Defines
    PdfState, PdfData, PdfMenuItem, PdfCategory, PdfDoc, and
    RenderPdfFn typedefs at module level. A single generic
    getTaggedAs<T>(api, tag) helper replaces two duplicate per-tag cast
    helpers.
  • src/_lib/types/eleventy.d.ts — Adds DietaryKey type and a
    non-optional dietaryKeys: DietaryKey[] field on MenuItemData. The
    11tydata boundary already guarantees [] here, so the PDF builder no
    longer needs ?? [] fallbacks.
  • src/_lib/types/index.d.ts — Re-exports DietaryKey.
  • scripts/strict-typecheck-ratchet.js — Lowers
    CURRENT_ERROR_COUNT from 395 to 374 and adds pdf.js to
    STRICT_CLEAN_FILES.
  • test/unit/build/pdf.test.js — Rewritten as 20 focused,
    behaviour-driven tests with shared fixture factories
    (menu(), category(), menuItem()). Removes a previous
    ALLOWED_MUTABLE_CONST exception that the old test file required.

Compromises

The json-to-pdf package types renderPdfTemplate as
(...) => unknown. To call .pipe() and .end() on the result with
strict typing, a cast to RenderPdfFn is required. That cast lives at
module level (where @type is permitted) and goes through unknown to
satisfy TypeScript's structural subtyping rules — it's the only type
assertion in the file.

pdfState is a module-level let (the same lazy-init pattern used
elsewhere in _lib/eleventy/), reset on each configurePdf call so
test ordering stays deterministic. configurePdf's registration
helpers (writePdfs, captureState, onAfter) are nested inside the
function so they pass single-use checks while keeping configurePdf
itself well under the 30-line limit.

Test plan

  • bun run precommit — all checks pass (lint, typecheck, strict
    typecheck, cpd, tests)
  • bun test test/unit/build/pdf.test.js — 20/20 pass

https://claude.ai/code/session_01AiUpnX7LR6e5KGmH2Ukoq6

claude and others added 5 commits April 19, 2026 10:19
Adds JSDoc type annotations so pdf.js passes tsc --strict. Ratchet
baseline drops from 395 to 374 errors and pdf.js joins
STRICT_CLEAN_FILES.

Supporting changes:
- Add `DietaryKey` type and a non-optional `dietaryKeys` field on
  `MenuItemData` (the 11tydata boundary already guarantees `[]`), so
  the PDF builder no longer needs `?? []` fallbacks.
- Rewrite pdf.test.js as 20 focused, behaviour-driven tests with shared
  fixture factories; this also removed the test file from
  `ALLOWED_MUTABLE_CONST`.

The cast that turns `json-to-pdf`'s `unknown`-returning `renderPdfTemplate`
into the typed `RenderPdfFn` is the only type assertion in the file and is
expressed at module level (where `@type` is permitted) via a double cast
through `unknown`, since the package types the return as `unknown`.
Resolves conflicts:
- scripts/strict-typecheck-ratchet.js: pdf.js entry was auto-merged
  into the updated STRICT_CLEAN_FILES list; CURRENT_ERROR_COUNT
  recomputed to 336 against the merged tree.
- test/unit/build/pdf.test.js: kept main's rewrite (more thorough,
  includes generateMenuPdf integration tests). All 25 tests pass
  against the strictly-typed pdf.js.
Main's pdf.test.js (taken in the prior merge) uses imperative
log/error capture arrays — restoring the allowlist entry that the
earlier branch had dropped because its alternative test rewrite
didn't need it.
The remote auto-merge (0923e32) left unresolved <<<<<<< / >>>>>>>
markers in scripts/strict-typecheck-ratchet.js and
test/unit/build/pdf.test.js, which is why CI biome was failing.
Resolves both:
- ratchet: keep the recomputed CURRENT_ERROR_COUNT = 336
- pdf.test.js: keep the version from this branch (main's rewrite,
  matched up with the strictly-typed pdf.js)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants