Design TypeScript types from requirements or an API description, including unions, optional fields, utility types and runtime guards for untrusted input.
Act as a TypeScript developer. Design types for the data described below and the runtime checks needed where the data crosses a trust boundary. If you only have sample JSON and want types inferred from it, use a sample-inference prompt instead. ## Inputs Data description, API documentation or schema (OpenAPI/JSON Schema excerpt allowed): [SPEC] Where the data comes from and whether it is trusted (user input, webhook, database, internal call): [SOURCE] TypeScript version and compiler options such as `strict` and `exactOptionalPropertyTypes`: [COMPILER] Existing validation library, if any (for example none, Zod, Valibot): [VALIDATION] ## Instructions 1. Model variants as discriminated unions with a literal discriminant; model enumerations as string literal unions unless the codebase uses `enum`. 2. Mark a property optional only when the spec says it may be absent, and nullable only when it may be `null`. 3. Add short JSDoc comments that state units, formats and constraints from the spec (for example "amount in minor units"). 4. For untrusted sources, write a type guard (`value is T`) or a schema with the supplied validation library that checks every property the code relies on. A type annotation alone does not validate runtime data. 5. Add utility types (`Pick`, `Omit`, `Partial`, mapped types) only where a real use is described, such as create vs update payloads. 6. Flag spec ambiguities instead of guessing. ## Return 1. Type declarations. 2. Runtime guard or schema for each untrusted entry point. 3. A usage example that narrows on the discriminant. 4. A table of guard test cases: input, expected true/false. 5. Open questions about the spec. ## Acceptance checks Declarations compile under the stated compiler options. Guard cases cover each variant, a wrong type in a required field and an unknown discriminant. Do not claim the guard tests passed unless they were run.
Illustrative input and expected result. This example is a verification guide, not a recorded model run.
Content reviewed .
Spec: payment webhook events. `payment.succeeded` has id (string), amountMinor (integer, minor currency units), currency (ISO 4217 code). `payment.failed` has id and failureCode (string). Other event types may be added later and must be ignored. Source: public webhook endpoint (untrusted) Compiler: TypeScript 5.9, strict Validation: none, hand-written guards
A `PaymentEvent` discriminated union on `type`; an `isPaymentEvent(value: unknown): value is PaymentEvent` guard that rejects null, non-integer `amountMinor` and unknown event types; a switch on `event.type` in the usage example; and a guard-case table including a fractional amount (false) and `payment.refunded` (false). Illustrative expectation, not a recorded model run; a reference guard with these five cases was executed locally (see report).
Ask questions or share concrete improvements.
No comments yet.
Sign in to join the discussion.
Guides for adapting and checking prompts:
Author-supplied tool and model tags; confirm behavior with your own examples.
IDEs
Models