{
  "id": 11842612,
  "title": "How to Convert JSON to TypeScript Interfaces (and Why Types Aren't Enough)",
  "url": "https://urgent.news/2026/10/04/how-to-convert-json-to-typescript-interfaces-and-why-types-arent",
  "topic": "tech",
  "section": "Tech",
  "published": "2026-10-04T04:30:00.000Z",
  "source": {
    "name": "Dev.to",
    "slug": "dev-to",
    "url": "https://dev.to/pulkitgovrani/how-to-convert-json-to-typescript-interfaces-and-why-types-arent-enough-4cnb"
  },
  "original_language": "en",
  "account": "When you retrieve data from an API using TypeScript, manually creating types from the JSON response can be time-consuming and prone to mistakes. However, a generator can swiftly convert a sample response into interfaces, providing a useful starting point—provided you understand its limitations.\n\nHere's an example of JSON to TypeScript conversion:\n- JSON: `{ \"id\": 7, \"name\": \"Ada\", \"email\": null, \"tags\": [\"a\", \"b\"], \"address\": { \"city\": \"Pune\" } }`\n- TypeScript Interfaces:\n```typescript\ninterface Address {\ncity: string;\n}\n\ninterface User {\nid: number;\nname: string;\nemail: null;\ntags: string[];\naddress: Address;\n}\n```\nNotice the `email` field is marked as `null`. The generator only observed a null value, so it cannot infer it's typically a string.\n\nTo refine the generated types:\n1. Convert null to the appropriate type when the field can hold other values.\n2. Use a question mark to mark optional fields that might be missing.\n3. Employ unions for fields that can store different data types.\n4. Explicitly convert dates to strings in JSON, as TypeScript has no native date type.\n5. Utilize integers or strings for fields with a fixed set of possible values.\n\nWhen deciding between `interface` and `type`, either can be used for object shapes. Use `type` for unions and `interface` for extending or merging declarations.\n\nIt's crucial to remember that TypeScript types are erased at runtime. While TypeScript won't check the data, it's essential to validate untrusted data at the boundary of your application using schema libraries like Zod or JSON Schema.\n\nAfter generating an initial interface from a sample response, refine nullable fields, optional fields, and unions based on the API documentation. Regularly update or regenerate types when the API changes. Be cautious about the data you paste into third-party sites, especially when working with real API responses that may contain sensitive information.",
  "summary": "When you consume an API in TypeScript, writing the types by hand from a JSON response is tedious and error-prone. A generator can turn a sample response into interfaces in seconds. It is a great starting point, as long as you understand what it can and can't know. From JSON to interface // JSON { \"id\" : 7 , \"name\" : \"Ada\" , \"email\" : null , \"tags\" : [ \"a\" , \"b\" ], \"address\" : { \"city\" : \"Pune\" }…",
  "key_points": [
    "JSON converted to TypeScript interfaces automatically",
    "Email field marked as null, not inferred as string",
    "Tips provided to refine generated types"
  ],
  "editors_take": "Using a generator to convert JSON to TypeScript interfaces streamlines development but requires manual refinement to accurately capture nullable fields, optional fields, and unions, ensuring robust data validation.",
  "illustration": null,
  "coverage": {
    "outlets": 1,
    "also_reported_by": []
  },
  "ai_generated": true,
  "disclaimer": "Summaries, key points and the editor’s take are written by software from other outlets’ reporting and may contain errors — always check the linked original."
}