Intermediate
Validate data at the boundary
JSON.parse gives any. Write parseUser(text: string): User | undefined that returns the user only if the data really is an object with a string name and a number age, without any as User cast and without any.
The program prints the result for three inputs. A good user prints Asha 30, a user with text for the age prints invalid, and non-JSON-object text [] prints invalid.
What Check tests
- Your code has no type errors
- The program prints Asha 30, invalid, invalid
- No type cast to User
- No any
- parseUser returns User or undefined
Stuck? One hint at a time
The solution unlocks after your first Check. Try first: that is where the learning happens.
Runs in your browser. Your code is saved here.
Press Run to see the page.
Console
Nothing printed yet.