// API response that should remain immutable
const apiResponse: JsonValue = {
data: {
users: [
{ id: 1, name: "Alice" },
{ id: 2, name: "Bob" }
]
},
meta: { total: 2, page: 1 }
};
// Type-safe JSON parsing
const parseConfig = (jsonString: string): JsonValue => {
return JSON.parse(jsonString) as JsonValue;
};
// apiResponse.data.users.push({...}); // ✗ Error: readonly array
Represents any valid JSON value in its immutable form. This includes primitives, readonly arrays of JSON values, or readonly objects where keys are strings and values are JSON values.
Use this type for representing parsed JSON data that should not be modified, such as configuration files, API responses that should remain unchanged, or when enforcing immutability in your application.