Essential Types And Notations 20 exercises

Debugging JSON Parsing

Consider the following code, which uses JSON.parse to parse some JSON:

const parsedData = JSON.parse<{
name: string; // red squiggly lines for the full {} argument
age: number;
}>('{"name": "Alice", "age": 30}');

There is currently an error under the type argument for `JSON.

Loading exercise


00:00 In this code here, we are json.parsing some json here, and this json has a name of Alice and an age of 30 on it. And we're parsing as well a type argument to json.parse, name and age here. And we're expecting the parse data to be typed

00:18 as kind of name, age, number, you know, what we've got here. But it's not, it's typed as something else. And your job here is to basically figure out why that's happening and decode the errors that are happening up here, and see if you can find an alternative way of expressing this that makes the type errors go away.

00:37 Good luck.