Beginner's TypeScript Tutorial (18 exercises)

Working with Arrays

Let's start looking at a bit more complex type declarations.

We've got a User type that is similar to what we've had before. We also have a new Post interface:

I've created a defaultUser that has a couple of posts added, but there's an error:

The problem is that in the User interface it's expecting a single Post instead of the array that defaultUser has.


Your challenge is to fix this type error by determining how to represent arrays.


Let's start looking at a bit more complex type declarations here. We've got a User type, which is very similar to what we had before, firstName, lastName, role, id, and we now added some posts to that user. Each post has an id, and it has a title.

I've got my defaultUser down here, but posts is yelling at me. It's saying type blah-blah blah-blah blah is missing the following properties from type Post, id, title.

It looks like here that it's expecting a single post, so what I've said to it is...I'm pretty sure it'll be happy if I just get rid of this and put in that. Yeah, it's not yelling at me anymore.

I want it to represent an array of posts, so your problem is to basically go into the TypeScript docs, work out how you can represent arrays, and come back and fix this type error.