0

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.

1 Answer 1

1

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

type TicketsItemMustHaveHistory = Required<TicketsItem>

Playground

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.