The Problem: JSON.parse Returns any
TypeScript's own type for JSON.parse is parse(text: string, reviver?): any. any switches type checking off, so a cast like as User is only a promise you make to the compiler. Nothing checks it when the code runs:
interface User {
id: number
name: string
email: string
}
const text = '{"id": 1, "fullName": "Emily Clark"}'
const user = JSON.parse(text) as User // compiles without a warning
console.log(user.name.toUpperCase())TypeError: Cannot read properties of undefined (reading 'toUpperCase')
The API sent fullName, not name. TypeScript believed the cast, so the mistake only showed up at runtime, often far from the line that caused it. The same is true for response.json() in fetch, which is typed as Promise<any>.
Safer: Parse to unknown
Give the result the type unknown instead. TypeScript then refuses to let you use it until you have checked it:
const data: unknown = JSON.parse(text) console.log(data.name)
error TS18046: 'data' is of type 'unknown'.
A type guard does the check by hand and tells TypeScript the result:
function isUser(value: unknown): value is User {
return (
typeof value === 'object' &&
value !== null &&
typeof (value as Record<string, unknown>).id === 'number' &&
typeof (value as Record<string, unknown>).name === 'string' &&
typeof (value as Record<string, unknown>).email === 'string'
)
}
const data: unknown = JSON.parse(text)
if (isUser(data)) {
console.log(data.name.toUpperCase()) // data is User here
} else {
console.log('not a valid user')
}For our example this prints not a valid user instead of crashing. Type guards need no library, but they get long for nested data, and nothing keeps them in step with the interface.
Best: Validate with Zod
A Zod schema checks the data at runtime and gives you the TypeScript type, so the two can never drift apart:
import { z } from 'zod'
const UserSchema = z.object({
id: z.number().int(),
name: z.string(),
email: z.email(),
})
type User = z.infer<typeof UserSchema>
const result = UserSchema.safeParse(JSON.parse(text))
if (result.success) {
console.log(result.data.name) // result.data is User
} else {
console.log(result.error.issues)
}name: Invalid input: expected string, received undefined email: Invalid input: expected string, received undefined
Use safeParse when bad data is expected and you want to handle it, or parse, which throws a ZodError, when bad data is a bug. You do not have to write the schema by hand: paste an example response into JSON to Zod and copy the result.
Typing a fetch Response
Wrap the call once, and every caller gets checked, typed data:
async function getUser(id: number): Promise<User> {
const response = await fetch(`https://api.example.com/users/${id}`)
if (!response.ok) throw new Error(`HTTP ${response.status}`)
return UserSchema.parse(await response.json())
}Only Need Types? Generate Them
If you trust the source, for example your own backend, interfaces may be enough. Writing them by hand for a large response is slow and error-prone; paste a sample into JSON to TypeScript to get every nested interface and optional field at once. Remember that these types disappear when the code is compiled, so they describe the data but never check it.
Comparing the Options
| Approach | Checks real data? | Best for |
|---|---|---|
JSON.parse(text) as User | No | Quick scripts with data you control |
unknown + type guard | Yes, by hand | Small shapes, no extra library |
| Zod schema | Yes | API responses, user input, config files |
| Generated interfaces | No | Typing data from a trusted source |
Importing a .json File
Turn on "resolveJsonModule": true in tsconfig.json and TypeScript reads the type straight from the file. In Node.js ES modules the import needs the with { type: 'json' } attribute:
import settings from './settings.json' with { type: 'json' }
const port: number = settings.port // typed from the file: { name: string; port: number }This is fine for config that ships with your code. For files that users or other programs change, read and validate them instead, as shown in Read a JSON File in Node.js.
Dates and Other Values Change on the Way
JSON has no date type, so JSON.stringify writes a Date as text, and JSON.parse gives you a string back, whatever your interface says:
const saved = JSON.stringify({ title: 'Launch', at: new Date('2026-11-28T09:30:00Z') })
// {"title":"Launch","at":"2026-11-28T09:30:00.000Z"}
typeof JSON.parse(saved).at // 'string'
const EventSchema = z.object({ title: z.string(), at: z.coerce.date() })
EventSchema.parse(JSON.parse(saved)).at instanceof Date // trueJSON.stringify also drops undefined values and functions: JSON.stringify({ a: 1, b: undefined, c: () => 1 }) gives {"a":1}. Values it cannot write at all, such as a BigInt or an object that refers to itself, throw; see Converting Circular Structure to JSON.
A Type for Any JSON Value
When the shape really is open, like a settings blob, describe "any valid JSON" precisely instead of using any:
type Json = string | number | boolean | null | Json[] | { [key: string]: Json }
const config: Json = { port: 8080, tags: ['a', 'b'], nested: { ok: true, none: null } }TypeScript now rejects dates, functions and undefined in config, because they are not JSON. To check that a string is valid JSON before you parse it, use the JSON Validator.
Frequently Asked Questions
What type does JSON.parse return in TypeScript?
It returns any, which turns type checking off. A cast such as JSON.parse(text) as User compiles, but nothing checks the data at runtime.
How do I safely type parsed JSON?
Parse into unknown and check it, either with a type guard function or, for real API data, with a Zod schema. A schema checks the data at runtime and gives you the TypeScript type through z.infer.
Why is my Date a string after JSON.parse?
JSON has no date type. JSON.stringify writes dates as ISO text and JSON.parse returns that text. Convert it back, for example with z.coerce.date() in a Zod schema.
How do I import a .json file in TypeScript?
Turn on resolveJsonModule in tsconfig.json; TypeScript then types the import from the file. In Node.js ES modules, write import data from './data.json' with { type: 'json' }.