A shared TypeScript contract can reduce mismatches between a frontend and backend, but compile-time types do not validate network input or grant access. A full-stack design still needs server-side parsing, authorization, and a stable error model.
Define ownership at the API boundary
NestJS can own application services and persistence while a tRPC router exposes selected procedures to the client. Keep the router thin: validate inputs, derive the caller from trusted session context, invoke the use case, and map its result. Do not duplicate domain rules in both the router and React component.
React Query manages client cache state around requests. Define query keys consistently, invalidate or update them after mutations, and handle loading, stale, and error states in the UI. Optimistic updates need a rollback path when the server rejects the mutation. Avoid caching sensitive data longer than the session and permissions allow.
Preserve runtime guarantees
Use schemas for runtime validation and infer types where possible so contract and implementation stay aligned. Authorization should check resource ownership on every server call. Keep secrets and server-only modules out of client imports. For non-TypeScript clients or external integrations, document the transport contract independently of inferred types.
Test unauthorized calls, invalid input, stale cache after mutation, and server failures. This stack is useful when end-to-end types reduce coordination costs; it is not a substitute for runtime checks or good API boundaries.
Trace type safety through a single publication request
A client can infer the result of a tRPC procedure, while its .input validator parses the values supplied at runtime. Put resource authorization in the procedure or service that knows the current actor. A NestJS application service can still own the business rule if the tRPC adapter calls it through a supported boundary. Avoid copying the publication rule into UI components that happen to call the endpoint. tRPC validators
After saving a revision, invalidate the queries whose projections changed, such as the article's editor state and its review list. TanStack Query invalidation marks matching data stale and can refetch active queries. Use an exact or scoped key so editing one draft does not refetch unrelated content. Surface conflict errors without erasing the text the writer still has in the editor. TanStack Query invalidation
Worked example
Share these contract shapes across server and client, then validate actual input at the server boundary. A generated client type cannot verify JSON received from an untrusted caller.
tsinterface CreateArticleRequest { title: string; content: string; } interface CreateArticleResponse { articleId: string; status: "DRAFT" | "REVIEW"; version: number; }




Community
Discussion
Loading comments…