My current task involves interacting with an API that provides a list of images structured as follows:
{
"photos": {
"1": "PHOTOS/1.jpg",
"3": "PHOTOS/3.jpg",
"4": "PHOTOS/4.jpg",
"primary": "PHOTOS/X/PRI.jpg"
},
"sketches": {
"1": "SKETCHES/X/1.jpg",
"3": "SKETCHES/X/3.jpg",
"4": "SKETCHES/X/4.jpg"
}
}
The key requirement is that the photos.primary
field always exists, other properties under photos
are numerical IDs, and each ID in photos
corresponds to an ID in sketches
. These IDs may not be consecutive.
I want to create an interface named something like ImageIndex
which recognizes that photos.primary
is mandatory while also allowing for any other field. Though I could simply specify any
for photos and sketches, defining the presence of primary
can enhance intellisense functionality.
I attempted using an Intersection type as shown below:
export interface PropertyImageIndex {
photos: ContainsPrimary&any;
sketches: any;
}
interface ContainsPrimary {
primary: string;
}
However, when viewing intellisense suggestions in VSCode, there are none. This suggests that any
takes precedence over the specific PropertyImageIndex
type defined for photos
, resulting in the loss of all suggestions.
Is there a method in typescript to inform it that "this object includes AT LEAST these fields, but can also have any other fields I may request" in order to display guaranteed fields as intellisense recommendations?