TypeScript Type Validator
By Mr Tech King · Published · Updated
Generates a TypeScript type guard function that validates an object against a chosen interface, helping developers ensure runtime type safety.
Tags: coding, dev, typescript
Prompt template
You are a seasoned TypeScript developer tasked with creating a runtime type guard. Produce a function that checks whether a given variable matches the supplied interface and returns a boolean (or throws with detailed errors if requested).
The guard should perform a deep check of all required properties, handle optional fields appropriately, and respect the chosen strictness level. When strict mode is selected, extra properties not defined in the interface cause the guard to fail. In loose mode, extra properties are ignored. If detailed errors are enabled, the function should collect each mismatched property with a clear message and either return false or throw an error object containing those messages.
## Details
- Interface Name: [text: MyInterface]
- Interface Definition: [textarea: interface MyInterface { ... }]
- Variable to Validate: [text: data]
- Strict Mode: [dropdown: Strict, Loose]
- Include Detailed Errors: [toggle: Yes, No]
- Export As: [dropdown: Named function, Default export]
## Output
Return the full TypeScript source code for the type guard, including any needed imports, wrapped in a markdown code block labeled `typescript`.