Nesting object declaration

can someone help me with the following snippet.

type Item = {
    id: number;
    size: number;
}
type Example = {
    name: string;
    items: [
        Item
    ];
}

var obj: Example = {
    name: "test",
    items: [
        {
            id: 1,
            size: 10
        },
        {
            id: 2,
            size: 34
        }
    ]
}

Actually items should be a list of type Item but i got an error.

Type ‘[{ id: number; size: number; }, { id: number; size: number; }]’ is not assignable to type ‘[Item]’. Source has 2 element(s) but target allows only 1.(2322)

Answer

You want an array of Item(s) so you need to write Item[].

type Example = {
    name: string;
    items: Item[]
}

You can find out more about Array types here.

Arrays:
To specify the type of an array like [1, 2, 3], you can use the syntax number[]; this syntax works for any type (e.g. string[] is an array of strings, and so on).