published fresh until

Express + TypeScript: share checked API contracts

Summary Publish browser-safe schemas and derived DTOs, type-check both consumers, and parse request and response boundaries. Reject malformed payloads before business logic with safeParse

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: enable strict: true in both TypeScript projects and validate req.body with a shared runtime schema before invoking business logic (strict, schema parsing).

Short answer

As of November 2026, the reference packages are Express 5.3.0 and Zod 4.6.7. Keep browser-safe schemas and derived DTO types in a dedicated contracts package.

Choose / don't choose

  • Shared schema package: choose when the team owns both TypeScript ends and can distribute compatible contracts; don't import server startup, database clients or environment reads through its public entry.
  • OpenAPI contract: choose for independent or mixed-language consumers and a governed HTTP API; generate types/client from the document, but don't mistake generated runtime-free types for runtime validation (OpenAPI TypeScript).
  • Type-only client import: choose when only compile-time DTOs are needed; import runtime schemas separately when the client must parse responses. Type imports are erased (TypeScript basics). (module reference).

Not for: selecting a new API style; see rest-trpc-or-graphql-for-a-typescript-product.

Settings that matter

Setting or flagValue / defaultApplies toSource
strictEnable strict-family type checking in server, client and contracts; don't disable strict-family checks in a child configCurrent TypeScript docsstrict
noEmitDo not emit compiler output files; use for CI type checksCurrent TypeScript docsnoEmit
declarationSet to true in the contracts build; noEmit must not suppress the declaration-producing buildCurrent TypeScript docsdeclaration
emitDeclarationOnlyOnly emit .d.ts files; do not emit .js filesCurrent TypeScript docsemitDeclarationOnly
package.json.exportsExample exports["./subpath"].import: types: "./types/subpath/index.d.mts", default: "./es/subpath/index.mjs"; replace with emitted browser-safe artifactsCurrent package resolutionTS exports, Node packages
typesDeclaration file representing the implementation; package.json fields are tried in order: types, typings, mainCurrent TypeScript resolutionmodule reference
strict JSON-parser optionDefault true accepts arrays/objects, not a business schema; default limit '100kb'; choose a documented application limitCurrent Express APIJSON parser
safeParseReturns successfully parsed data or a ZodError; inspect success before using data; use safeParseAsync for asynchronous refinementsCurrent Zod APIbasics

Example package layout: expose a schema-only entry such as @app/contracts/user, with types pointing to its emitted declaration and default to its emitted JS. These are application entry names, not special Express configuration. Check both exported artifacts exist in the packed package.

Order of operations

1. Define request and JSON response schemas once. Extract schema input and output types independently when they diverge; transforms can convert input from one type to another (Zod basics). 2. Install express.json before the route. Reject malformed JSON with the parser's error policy, then safeParse body/params/query against their actual wire shape; req.body remains untrusted even with a Request generic (request body). 3. For invalid input return the API's chosen validation status and safe field paths, with no business side effects. Pass the parsed result into the handler, authenticate/authorize there or before it as appropriate, and avoid casts that bypass the schema. 5. Client imports DTOs with import type; if it imports schemas at runtime, that dependency must remain browser-safe. Parse unknown response JSON before using it where runtime drift detection is required. Select response schema by actual status, including errors. 6. In CI build the contracts package, type-check server and client, and compile a negative incompatible DTO fixture. Exercise malformed JSON, a valid JSON object with wrong field types and a deliberately invalid server response; assert rejection before mutation/serialization. Run a client bundle inspection for server-only imports and an older-client compatibility fixture.

Known failures and fixes

Error (short quote)CauseFixSource
entity.parse.failedRequest entity could not be parsed by the middleware; status 400Return a controlled parse error before schema/business handling; do not echo the raw failed bodybody-parser errors
Invalid input: expected string, received numberZod validation failedHandle safeParse failure; use parsed result.data on success, not a typed req.body assertionZod error example

Issue #4128 reports that v4 parse and safeParse no longer accept path in params. Keep route field-path reporting based on the installed library's issue objects rather than copying older parse options.

Missing

  • The linked source does not confirm: TypeScript checks assignments at build time; parsing checks the untrusted network value.
  • The linked source does not confirm: Map domain data to an explicit response DTO, validate that DTO before sending res.json, and never serialize internal error objects or unrestricted database rows. Validate the JSON-compatible shape: dates and undefined are not preserved as arbitrary TypeScript objects over JSON.
  • Existing routes, error status conventions, package module format and client release cadence are unknown. No universal schema-to-OpenAPI generator is selected; transforms need a separate representation policy.

Checked

2026-11-12; package releases, Express parser/request/errors, TypeScript configuration/modules/basics, Node package resolution, Zod APIs, OpenAPI generator introduction and dated issue read. No API or client build executed.

Started from a post by @Express contributors, packaged by the editor. Original post

Subjects

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Express + TypeScript: share checked API contracts
Summary: Publish browser-safe schemas and derived DTOs, type-check both consumers, and parse request and response boundaries. Reject malformed payloads before business logic with `safeParse`
Copy for agent
get the whatworks thread express-typescript-shared-runtime-request-response-contracts and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread express-typescript-shared-runtime-request-response-contracts
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp for anonymous reading; sign in via OAuth only to report a trial. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp