4

I have this AjaxResponse Interface :

interface AjaxResponse {
    calendar: {
        dates: {
            date: string,
            hours: {
                hour: string,
                id: number,
                status: string,
                client: {
                    name: string,
                    surname: string,
                    email: string,
                    phone: string,
                    id: number
                }
            }[]
        }[]
    },
    rights: string,
    listenerUrl: string
}

I will iterate through a list of dates and edit some of them :

let dates: AjaxResponse['calendar']['dates']
let i: number
let l: number

    receive(response: AjaxResponse) {
                dates = response.calendar.dates
                l = dates.length
                for (i; i < l; i++) {
                    calendar.editDates(dates[i])
                }

The edit dates function is as follow :

editDates(newDate: AjaxResponse['calendar']['dates']) {
    }

The dates[i] won't compile because my last function is expecting an array. How could I declare a single date rather than list of dates in my last function ?

Edit : Found a working solution by creating two interfaces :

interface AjaxResponse {
    calendar: {
        dates: AjaxResponseDate[]
    },
    rights: string,
    listenerUrl: string
}

interface AjaxResponseDate {
    date: string,
    hours: {
        hour: string,
        id: number,
        status: string,
        client: {
            name: string,
            surname: string,
            email: string,
            phone: string,
            id: number
        }
    }
}

1 Answer 1

3

To get the type of an item in an array, you can use a type query by number:

function editDates(newDate: AjaxResponse['calendar']['dates'][number]) {
}

The solution to have t interfaces is also a possible one, but you can keep a single type if you want.

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

1 Comment

Yes both can be use, but your answer fits my interrogation perfectly. Thanks

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.