Moon
Moon

Reputation: 890

Typescript get the type of object from object array

In Typescript, I have created a TiketsItem and the interface with that TiketsItem array. How can I get the type from arr which must have history key?

interface TicketsItem {
    id: number
    status: string
    addedAt: string
    subject: string
    history?: {
        date: string
        message: string
        messageBy: string
    }[]
}

export interface Tickets extends Array<TicketsItem> { }

const arr: Tickets = [
    {
        "id": 1,
        "subject": "Email   certificate expired",
        "status": "Client response pending",
        "addedAt": "2020-09-23",
        "history": [
            {
                "date": "20-10-10",
                "message": "sjflsjl",
                "messageBy": "client"
            },
        ]
    },
    {
        "id": 2,
        "subject": "Issue with Email certificate",
        "status": "Client response pending",
        "addedAt": "2020-09-23"
    },
]

const ticketOne = arr[0]

I'm trying to use

const ticketOne = arr[0]
type TicketsItemMustHaveHistory = typeof ticketOne

but it returns TicketsItem again.

Upvotes: 0

Views: 106

Answers (1)

akuiper
akuiper

Reputation: 215117

You can just use Required to make all properties in TicketsItem type required:

type TicketsItemMustHaveHistory = Required<TicketsItem>

Playground

Upvotes: 1

Related Questions