Beginner's TypeScript Tutorial (18 exercises)
Problem

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:

interface User {
  id: number;
  firstName: string;
  lastName: string;
  role: "admin" | "user" | "super-admin";
  posts: Post;
}

interface Post {
  id: number;
  title: string;
}

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

export const defaultUser: User = {
  id: 1,
  firstName: "Matt",
  lastName: "Pocock",
  role: "admin",
  posts: [
    {
      id: 1,
      title: "How I eat so much cheese",
    },
    {
      id: 2,
      title: "Why I don't eat more vegetables",
    },
  ],
};

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

Challenge

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

Transcript

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.