Urgent.News

What's breaking now, across thousands of outlets.

Tech

How to Convert JSON to TypeScript Interfaces (and Why Types Aren't Enough)

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" }…

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.

Here's an example of JSON to TypeScript conversion:

- JSON: `{ "id": 7, "name": "Ada", "email": null, "tags": ["a", "b"], "address": { "city": "Pune" } }`

- TypeScript Interfaces:

```typescript

interface Address {

city: string;

}

interface User {

id: number;

name: string;

email: null;

tags: string[];

address: Address;

}

```

Notice the `email` field is marked as `null`. The generator only observed a null value, so it cannot infer it's typically a string.

To refine the generated types:

1. Convert null to the appropriate type when the field can hold other values.

2. Use a question mark to mark optional fields that might be missing.

3. Employ unions for fields that can store different data types.

4. Explicitly convert dates to strings in JSON, as TypeScript has no native date type.

5. Utilize integers or strings for fields with a fixed set of possible values.

When deciding between `interface` and `type`, either can be used for object shapes. Use `type` for unions and `interface` for extending or merging declarations.

It'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.

After 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.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

Surfer SEO Pricing Explained: Which Plan Actually Makes Sense in 2026?

TL;DR Surfer SEO pricing runs $49 / $99 / $182 / $299 per month on annual billing for Discovery, Standard, Pro and Peace of Mind — or $59 / $119 / $219 / $359 if you pay monthly.

  • Surfer SEO offers four plans in 2026: Discovery, Standard, Pro, and Peace of Mind.
  • Discovery plan ($49/month) allows 120 documents/year for solo creators testing Surfer.
  • Peace of Mind plan ($299/month) provides unlimited documents under fair use with 10 seats.

Ten hours unattended: what a long job needs to survive alone

Development covered 5 Aug 2026 to 7 Aug 2026 (commit dates). A lot of the work on this project runs overnight with nobody watching.

  • The development project ran from August 5, 2026, to August 7, 2026.
  • The job operates unattended for ten hours, generating reports and commits.
  • Measuring total browser resources is crucial to prevent slowdowns.

Pixel Helper cannot see a Conversions API POST. A 200 is not the contract.

A checkout worker posts a Purchase to the Meta Conversions API. The response is HTTP 200. Test Events stays empty. The next move is usually a new access token. The token is rarely the bug.

  • Pixel Helper fails to detect POST requests from Conversion API
  • HTTP 200 status code indicates server is alive, not data validation
  • Proper JSON formatting and required fields must be verified separately

More from Sunday 4 October →