---
name: typescript-type-validator
description: "Generates a runtime type guard that proves an unknown value matches your interface: deep checks on every required field, optional fields handled, strict or loose on extras, and errors that name the exact property that failed. Use when data from an API, form, or file arrives as unknown and you need a type guard that checks it deeply against an existing interface at runtime. Do not use for generating the interface itself, validating with a schema library, or building a validating store. Related topics: coding, typescript."
---

# TypeScript Type Validator

Generates a runtime type guard that proves an unknown value matches your interface: deep checks on every required field, optional fields handled, strict or loose on extras, and errors that name the exact property that failed.

## When to use

Use this skill when data from an API, form, or file arrives as unknown and you need a type guard that checks it deeply against an existing interface at runtime. Do not use it for generating the interface itself, validating with a schema library, or building a validating store.

## Workflow

1. Ask the user for each input listed below in a single message, skipping any they have already given. Where options are listed, offer them, but accept other answers too.
2. Write a helper guard per nested type that checks primitives, literals, arrays element by element, and unions variant by variant.
3. Write the main guard with a type predicate return type that rejects null and non-objects first, then checks every required property.
4. Validate optional properties only when present and apply the strict or loose rule for undeclared keys.
5. When detailed errors are on, collect every failure as path plus message and expose them through an assert variant or error list.
6. Confirm the guard compiles against the pasted interface and narrows the variable at a call site.

## Inputs

- **Interface name** (free text, e.g. "OrderPayload")
- **Interface definition** (longer free text)
- **Variable to validate** (free text, e.g. "payload")
- **Extra properties** (choose one: Strict (reject unknown keys), Loose (ignore unknown keys))
- **Include detailed errors** (choose one: Yes, No)
- **Export as** (choose one: Named function, Default export)

## Output

One typescript code block with helper guards, the main type-predicate guard, and the assert or error-list variant when requested; no prose outside it.

## Prompt template

Fill the inputs into this template and follow it as the full instruction.

### TypeScript Type Validator

Write a runtime type guard for the interface below.

- Interface name: {{Interface name}}
- Interface definition: {{Interface definition}}
- Variable to validate: {{Variable to validate}}
- Extra properties: {{Extra properties}}
- Include detailed errors: {{Include detailed errors}}
- Export as: {{Export as}}

Write the guard as a function whose return type is a type predicate (value is InterfaceName) so callers get narrowing. Check structurally and deeply: every required property is present with the right primitive, literal, array element, or nested object type; optional properties are validated only when present and not undefined; unions are checked variant by variant; arrays check every element. In strict mode, collect any key not declared on the interface as an error. Guard against null and non-object values before touching properties.

When detailed errors are requested, collect every failure as a path plus message (items.2.price: expected number but got string) rather than stopping at the first, and expose them: return false from the predicate and attach the list to a second exported function or to an assert variant that throws one Error listing them all. Write a helper per nested type so the guard stays readable and each helper can be reused.

### Output
Return the complete TypeScript source in one code block tagged typescript: imports if any, helper guards for nested types, the main guard with a type predicate return type, and the assert or error-list variant when detailed errors are enabled.

## Pitfalls

- Returning boolean instead of a type predicate, so the caller gets no narrowing from the guard.
- Treating an optional property set to undefined as a type error, which rejects values the interface allows.
- Checking only the first element of an array, which lets a malformed item through into typed code.
