I'm working with Giphy's API with Typescript and the response give me a mixed media array of objects, something I can summarize like this:
interface Type {
width: number;
height: number;
}
interface Image extends Type {
url: string;
}
interface Video extends Type {
videoUrl: string;
}
interface Media {
typeA: Image;
typeB: Image & Video;
}
I created a getUrl function that allows the user to select the url setting the media type (typeA or typeB) and the url type (url or videoUrl) but, as you can see, not all the combinations are valid (I can't select videoUrl for typeA media).
const getUrl = (media, mediaType: 'typeA' | 'typeB', mediaUrl: 'url', 'videoUrl'): string => {
return media[mediaType][mediaUrl];
};
This is facing me some typescript errors, like:
Element implicitly has an 'any' type because expression of type '"url" | "videoUrl"' can't be used to index type 'Image | (Image & Video)'. Property 'videoUrl' does not exist on type 'Image | (Image & Video)'.ts(7053)
Can someone help me to improve the type checking and fixing the errors?