Check out a free preview of the full TypeScript 5+ Fundamentals, v4 course

The "JSON Type Exercise" Lesson is part of the full, TypeScript 5+ Fundamentals, v4 course featured in this preview video. Here's what you'd learn in this lesson:

Mike provides an exercise where the goal is to define a type that accepts any valid JSON value. The solution, explaining the different types and their relationships and demonstrating how to pass all the test cases is also provided.


Transcript from the "JSON Type Exercise" Lesson

>> Let's attempt our first exercise. We're going to define a type that accepts any valid JSON value, and I've extracted the important bit out of the JSON specification. This describes what is allowed in JSON. So JSON value must be an object, array, number, or string, or one of these three literal names, false, true, or null.

This is a 100 percent of what's allowed in JSON. You don't see functions here. You don't see classes. Those are things that are allowed in JavaScript. You don't see undefined, for example, that is not a JSON concept. So you have some starting point code here. This is an important line to observe.

Everything below this line, you can think of as your test suite. These are a bunch of test cases to see if you have the following valid JSON things are valid down here. These are negative test cases, they should fail to compile. And we're getting yelled at here with ts-expect- error because it should fail to compile and it's not failing to compile right now.

So think of this almost like an inverse, like you need to make this throw or reject compiling not throw, but you need to make it, have a type error in order to make this test pass. So in the end, you want everything below this line all green, right?

Where you can see all of the things that should fail, will fail, and all of the things that should pass, pass. In terms of doing this, there are two ways you could do it. One is to click this try button, which will launch you into the TypeScript playground.

And this is a totally freestanding piece of code. You can see your red squiggles down here. You can code above this line to attempt this. Also, if you go into your workshop project, you'll notice we have chapter 8 in our notes. And then this gives you the exact same thing down here.

What you'll wanna do is just, all of these TS expect error things, oops, you'll want to do one level of uncommenting so they light up and yell at you, as they should, because right now there's no compile error and we want there to be a compile error. Please feel free to attempt this on your own.

Don't write any code below this line. You should just be defining these types here, and we'll see you and we'll walk through a solution. Let's go through the solution to the JSON types exercise. I'm gonna actually close this bottom terminal. We won't need it. All right, so let's try to solve this in pieces.

First, let's work with this JSONObject. And we might create a new type here as well. It's not necessarily gonna be the ones that sort of add up to this end thing, but we can call something a JSON primitive. And there we go. Copilot knows exactly what that means.

Maybe I'll just like solve this with copilot, where you just said JSONValue. That's a valid tool in the toolbox, but we'll talk through it anyway. So let's look at our checklist here. These are really the primitive JavaScript types that are possible in JSON. We've got string, we've got number, we've got Boolean, these two, and null.

All right, now, a JSONObject, we can say that that is, wow, that is using an index signature, and we can say for any key, there's a JSONValue. Now, we haven't defined what JSONValue is, but you can imagine, you could have a number here, or another object, or an array of objects.

So any valid JSONValue can be in one of these JSON objects. And in array, it's gonna be something very similar. It's just an array of JSONValues. And then what is a JSONValue in the end? Well, it's primitives, Or objects, or arrays. Let's check our tests. All the positive ones pass and no red squiggles for the failing ones.

So we could have avoided creating this type. If we wanted, we could have just done this, and it would have been perfectly fine. But I don't know, I kind of like the explicitness of this and it makes this last type very easy to think about. We can kind of think about the three different classes of JSONValues that we could have.

So this is a recursive type. Why? Well, object is defined in terms of JSONValue, which is defined in terms of object and the same is true with array. But it's all possible, and we can use this to define any kind of valid piece of JSON that we might have.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now