Free 40-page Claude guide — setup, 120 prompt codes, MCP servers, AI agents. Download free →
CLSkills
Code Generationintermediate

Type Generator

Share

Generate TypeScript types from JSON/API responses

Works with OpenClaude

You are a TypeScript code generation specialist. The user wants to automatically generate type definitions from JSON data or API responses.

What to check first

  • Verify you have typescript installed: npm list typescript
  • Check if you're using json-schema-to-typescript or building a custom generator
  • Identify the JSON structure's depth and complexity (flat objects vs nested structures)

Steps

  1. Install the required package: npm install --save-dev json-schema-to-typescript or use quicktype CLI
  2. Prepare your JSON sample file (e.g., response.json) with representative data including all property types
  3. Convert JSON to JSON Schema using a tool like json-schema-from-json or manually define the schema
  4. Run the generator with proper options: npx json2ts --input response.json --output types.ts
  5. For API responses, extract a real response payload and validate it matches your data structure
  6. Configure strict null checks in tsconfig.json to ensure strictNullChecks: true
  7. Review generated types for any unknown or any types that need refinement
  8. Add custom type refinements for discriminated unions or branded types if needed

Code

import fetch from 'node-fetch';
import { compile } from 'json-schema-to-typescript';
import * as fs from 'fs';

async function generateTypesFromAPI(apiUrl: string, outputPath: string) {
  // Fetch real API response
  const response = await fetch(apiUrl);
  const data = await response.json();

  // Convert response to JSON Schema
  const schema = {
    $schema: 'http://json-schema.org/draft-07/schema#',
    type: 'object',
    properties: Object.keys(data).reduce((acc, key) => {
      const value = data[key];
      acc[key] = inferType(value);
      return acc;
    }, {} as Record<string, unknown>),
    required: Object.keys(data),
    additionalProperties: false,
  };

  // Generate TypeScript types
  const typeContent = await compile(schema, 'APIResponse', {
    bannerComment: '// Auto-generated types from API response',
    enableConstEnums: false,
    strictIndexSignatures: true,
  });

  // Write to file
  fs.writeFileSync(outputPath, typeContent);
  console.log(`✓ Types generated at ${outputPath}`);
}

function inferType(value: unknown): Record<string, unknown> {
  if (value === null) return { type: 'null' };
  if (typeof value === 'boolean') return { type: 'boolean' };
  if (typeof value === 'number') return { type: 'number' };
  if (typeof value === 'string') return { type: 'string' };
  if (Array.isArray(value)) {
    return {

Note: this example was truncated in the source. See the GitHub repo for the latest full version.

Common Pitfalls

  • Treating this skill as a one-shot solution — most workflows need iteration and verification
  • Skipping the verification steps — you don't know it worked until you measure
  • Applying this skill without understanding the underlying problem — read the related docs first

When NOT to Use This Skill

  • When a simpler manual approach would take less than 10 minutes
  • On critical production systems without testing in staging first
  • When you don't have permission or authorization to make these changes

How to Verify It Worked

  • Run the verification steps documented above
  • Compare the output against your expected baseline
  • Check logs for any warnings or errors — silent failures are the worst kind

Production Considerations

  • Test in staging before deploying to production
  • Have a rollback plan — every change should be reversible
  • Monitor the affected systems for at least 24 hours after the change

Quick Info

Difficultyintermediate
Version1.0.0
AuthorClaude Skills Hub
codegentypescripttypes

Install command:

curl -o ~/.claude/skills/type-generator.md https://claude-skills-hub.vercel.app/skills/code-generation/type-generator.md

Related Code Generation Skills

Other Claude Code skills in the same category — free to download.

Want a Code Generation skill personalized to YOUR project?

This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.