I have a type SearchResultTrack, it consists of several other interfaces:
export enum Origin {
Youtube = "youtube",
Spotify = "spotify",
Soundcloud = "soundcloud",
}
export enum SearchResultType {
Track = "track",
Playlist = "playlist",
}
export enum QueryType {
Url = "url",
Search = "search",
}
interface BaseTrack {
name: string;
author: string;
url: string;
thumbnail: string;
estimatedDuration: number;
}
interface PlaylistInfo {
name: string;
author: string;
url: string;
tracksNum: number;
thumbnail: string;
}
interface SearchResultTrackBase {
origin: Origin;
type: SearchResultType.Track;
query: {
text: string;
type: QueryType;
};
}
export interface SearchResultUrlTrack extends SearchResultTrackBase, BaseTrack {
query: {
text: string;
type: QueryType.Url;
};
}
export interface SearchResultUrlPlaylistTrack extends SearchResultTrackBase, BaseTrack {
query: {
text: string;
type: QueryType.Url;
};
playlistInfo: PlaylistInfo;
}
export interface SearchResultSearchTrack extends SearchResultTrackBase {
query: {
text: string;
type: QueryType.Search;
};
variants: BaseTrack[];
}
export type SearchResultTrack = SearchResultUrlTrack | SearchResultUrlPlaylistTrack | SearchResultSearchTrack;
And a have a function:
function formatTrack(rawTrack: SearchResultTrack) {
if (rawTrack.query.type === QueryType.Url) {
console.log(rawTrack.name);
}
}
The problem is that it throws an error:
Property 'name' does not exist on type 'SearchResultTrack'.
Property 'name' does not exist on type 'SearchResultSearchTrack'.
I think that I need to have some more advanced type checking, but I don't know how to do it. I tried asking ChatGPT several times with different prompts but I didn't get anything useful.