Build Advanced TypeScript Utility Types for Type-Safe Development

Create advanced TypeScript utility types including deep manipulation, branded types, and discriminated unions with tests.

๐Ÿ“ The Prompt

Create a comprehensive TypeScript utility types library for a [PROJECT_TYPE] project that enforces type safety across [DOMAIN_CONTEXT] (e.g., API responses, form handling, state management). **Utility Types to Implement:** 1. **API Layer Types**: - `ApiResponse<T>`: A discriminated union type with `success` and `error` variants, where the success variant contains `data: T` and the error variant contains `code: number` and `message: string` - `PaginatedResponse<T>`: Extends `ApiResponse` with `meta: { page, perPage, total, totalPages }` - `EndpointConfig<TParams, TBody, TResponse>`: Maps route parameters, request body, and response type for [NUMBER] endpoints: [ENDPOINT_LIST] 2. **Deep Manipulation Types**: - `DeepPartial<T>`: Recursively makes all properties optional - `DeepReadonly<T>`: Recursively makes all properties readonly - `DeepRequired<T>`: Recursively makes all properties required - `PathKeys<T>`: Extracts all valid dot-notation paths from a nested object type (e.g., `'user.address.city'`) - `PathValue<T, P>`: Resolves the type at a given dot-notation path 3. **Domain-Specific Types**: - `FormState<T>`: Maps each field of `T` to `{ value: T[K], error: string | null, touched: boolean, dirty: boolean }` - `StrictOmit<T, K>`: Like `Omit` but enforces that `K` must be actual keys of `T` - `RequireAtLeastOne<T, Keys>`: Ensures at least one of the specified keys is present - `MutuallyExclusive<T, K1, K2>`: Ensures only one of two property groups can exist 4. **Validation & Guards**: - Write type guard functions for each `ApiResponse` variant - Create a `assertNever(x: never)` exhaustiveness checker - Implement a `Brand<T, B>` type for nominal typing of [BRANDED_TYPES] (e.g., UserId, EmailAddress) For each utility type, provide: the implementation, a JSDoc comment explaining usage, and at least 2 test cases using `type-testing` patterns (Expect<Equal<...>>) to prove correctness.

๐Ÿ’ก Tips for Better Results

Specify your TypeScript version (4.9+, 5.x) since newer versions support features like `satisfies` and `const` type parameters. Provide a real interface from your project as an example so the types are tailored to your data shapes. Ask for `.d.ts` declaration files if you plan to publish these as a shared package.

๐ŸŽฏ Use Cases

TypeScript developers and library authors use this when building type-safe foundations that eliminate runtime errors through compile-time checks across large codebases.

๐Ÿ”— Related Prompts

๐Ÿ’ป Coding beginner

Explain Code Like Im a Beginner

Get any code explained in plain English with line-by-line breakdowns, analogies, and learning suggestions.

๐Ÿ’ป Coding beginner

Debug My Code and Explain the Fix

Get your code debugged with clear explanations of what went wrong and why, plus the corrected version.

๐Ÿ’ป Coding intermediate

Write Unit Tests for My Code

Generate thorough unit tests covering edge cases, error handling, and both positive and negative scenarios.

๐Ÿ’ป Coding intermediate

Convert Code Between Languages

Convert code between any programming languages while maintaining idiomatic patterns and best practices.

๐Ÿ’ป Coding intermediate

Write a REST API Endpoint

Generate production-ready REST API endpoints with validation, error handling, and documentation.

๐Ÿ’ป Coding advanced

Create a GitHub Actions CI/CD Workflow for Automated Testing and Deployment

Generate a complete GitHub Actions CI/CD workflow with build, test, deploy, and notification jobs for your project.