TypeScript · Middle · Question 41 of 75

How do you type the result of JSON.parse safely?

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.

Further reading

Problem with a question

TypeScript · Middle · question 41

Test yourself

Take the TypeScript · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz