TypeScript · Middle · Question 41 of 75
How do you type the result of JSON.parse safely?
- ✓Correct answer: Treat the result as
unknownand validate it with a schema (Zod) - 2Just write
JSON.parse(text) as User- that's enough - 3Leave it as
any: TypeScript will check the structure later itself
Explanation
JSON.parse returns any, but external data may not match expectations. A schema (z.object({...})) validates data at runtime, and z.infer<typeof schema> gives a TS type without duplication. as User only hides the problem.