Verified against ChatGPT · 2026-09-26
Make a TypeScript API Fully Type-Safe
Refactor a TypeScript API integration to eliminate unsafe types, improve compile-time guarantees, and make data handling easier to maintain for developers working on production applications.
The prompt
Ready to copy — highlighted parts are example details you can swap.
You are a TypeScript engineer who specializes in building strongly typed, production-ready applications with safe APIs and maintainable data models. Refactor a TypeScript REST API client to make it as type-safe, predictable, and maintainable as possible. 1. Identify unsafe patterns such as `any`, unsafe assertions, nullable assumptions, untyped API responses, and inconsistent interfaces. 2. Create or improve TypeScript types, interfaces, unions, generics, and type guards where they provide meaningful compile-time guarantees. 3. Ensure external data is validated before being trusted by the application, especially a third-party REST API. 4. Preserve the existing behavior and public API unless a change is required to fix a genuine type-safety problem. 5. Explain the important changes and identify any remaining type-safety risks that cannot be eliminated without changing the architecture. Output the result in this format: **Improved Code**, followed by **Type-Safety Changes**, followed by **Remaining Risks**. Keep the explanation concise, use production-quality TypeScript, do not use `any` unless absolutely unavoidable, and do not include unrelated refactoring.
Customize
Optional — swap in your own details for the highlighted parts above.
Why this works
The prompt combines role-setting, explicit type-safety goals, architectural constraints, and a fixed output structure instead of simply asking for a refactor. It specifically targets common TypeScript weaknesses such as `any`, unsafe assertions, and unvalidated external data while requiring behavior preservation.
What you get back
Improved Code
type User = { id: string; name: string; email: string; };
function isUser(value: unknown): value is User { if (typeof value !== "object" || value === null) return false;
const user = value as Record<string, unknown>;
return ( typeof user.id === "string" && typeof user.name === "string" && typeof user.email === "string" ); }
Type-Safety Changes
- Replaced `any` with `unknown`.
- Added a runtime type guard for external data.
- Prevented invalid API responses from entering application logic.
Remaining Risks
- Runtime validation does not guarantee semantic correctness of the API data.
- Schema validation may be preferable for larger payloads.
Verified against
ChatGPT GPT-5.6 Luna · 2026-09-26
Building this for real?
This is a free starting point. If you'd rather have what Scult builds built and running for your business, that's Scult's day job.
EXPLORE WHAT SCULT BUILDS
