wasAsked.

How do you keep frontend TypeScript contracts synchronized with ASP.NET Core request and response models?

Paytm · Full Stack Developer · 3-4-years · technical

Published

Year not specified · .NET, TypeScript, ASP.NET Core

Community answers

Posted by wasAsked editorial collection

Make the API contract the source of truth and generate TypeScript clients/types from an OpenAPI document in CI. A breaking schema change should fail the consumer build before deployment. Example: // generated usage const order: OrderDto = await client.orders.getById(id); Keep generated files read-only and wrap them with small application adapters. Use nullable/required annotations consistently, stable enum serialization, and contract tests for important examples. Publish versioned specs and compare them for breaking changes in CI. During rolling releases, use additive changes first: deploy tolerant readers, then writers, migrate stored data if needed, and remove old fields only after every consumer has moved.

Backbencher Developer · · 0 likes

Hand-written TypeScript interfaces drift away from C# models over time. The reliable approach is to generate them from a single source of truth — the API's OpenAPI document. 1. Publish an accurate OpenAPI spec - Use Microsoft.AspNetCore.OpenApi (built into .NET 9+) or Swashbuckle/NSwag. - Annotate endpoints with response types (Produces<T>, ProducesProblem, TypedResults) so every status code and model appears in the spec. - Mark nullability correctly (nullable reference types enabled) and use the "required" keyword so required properties are explicit. - Configure enums to serialize as strings if the client should see names. 2. Generate the client - openapi-typescript generates types only; orval, NSwag or Kiota generate typed clients and hooks (including TanStack Query hooks). - Run generation as part of the build: the API project emits the spec at build time (for example Microsoft.Extensions.ApiDescription.Server), and the frontend runs "npm run generate:api". - Commit generated files, or generate in CI, and fail the build if the committed output differs from a fresh generation. 3. Catch breaking changes - Diff the spec in pull requests (oasdiff or openapi-diff) to flag removed fields, renamed properties or changed types. - TypeScript compilation then fails wherever the frontend relied on something that changed. - Contract tests against a running API validate that real responses match the spec. 4. Versioning and compatibility - Prefer additive changes: add optional fields instead of renaming. - Deprecate before removing, and keep old versions alive while clients migrate (API versioning). 5. Runtime safety - Types are erased at runtime, so validate critical responses with zod or similar if the API is not fully trusted. - Keep date handling consistent: ISO 8601 strings in JSON, converted explicitly in the client. Alternative for full-stack TypeScript teams: share types in a monorepo package — but with a .NET backend, OpenAPI generation is the standard, tool-supported route.

Ravindra Wadile · · 0 likes

Sign in to add your answer

Related interview topics