Convert JSON Arrays of Objects to TypeScript Types

Learn how to convert JSON arrays containing objects into TypeScript arrays with typed elements. Covers homogeneous and heterogeneous arrays.

Basic Conversions

Detailed Explanation

Typing Arrays in TypeScript

When JSON contains an array of objects, the converter inspects every element to determine a unified type for the array items.

Example JSON

{
  "users": [
    { "id": 1, "name": "Alice", "role": "admin" },
    { "id": 2, "name": "Bob", "role": "editor" },
    { "id": 3, "name": "Carol", "role": "viewer" }
  ]
}

Generated TypeScript

interface User {
  id: number;
  name: string;
  role: string;
}

interface Root {
  users: User[];
}

How Element Types Are Inferred

The converter merges all array elements into a single type. If every element has the same keys and value types, the result is a clean interface. When elements differ the converter takes the union:

[
  { "id": 1, "name": "Alice" },
  { "id": 2, "name": "Bob", "email": "bob@test.com" }
]

Here, email only appears in some elements, so it becomes optional:

interface Item {
  id: number;
  name: string;
  email?: string;
}

Primitive Arrays

Arrays of primitives map directly:

JSON TypeScript
[1, 2, 3] number[]
["a", "b"] string[]
[true, false] boolean[]
[1, "two", true] (number | string | boolean)[]

Nested Arrays

Arrays of arrays become T[][]. For example, a matrix [[1,2],[3,4]] becomes number[][].

Working with arrays is one of the most frequent conversion tasks because most API list endpoints return paginated arrays of resources.

Use Case

You fetch a list of users from a paginated API endpoint and need to type the response array so you can safely iterate over it with .map() and .filter().

Try It — JSON to TypeScript

Open full tool