Reputation: 4394
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
}
}
}
Upvotes: 4
Views: 4015
Reputation: 249606
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.
Upvotes: 3