Answer the question
In order to leave comments, you need to log in
Why can't TS define an interface and type variables?
Bug: TS2696: The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead? Type 'Object' is missing the following properties from type 'Movie': id, overview, poster_path, release_date, title
itself is an error in the loop and marked with a comment
interface Resp {
results: Object[],
page: number
}
interface Movie {
adult?: boolean;
backdrop_path?: string,
genre_ids?: Number[],
id: number,
original_language?: "en",
overview: string,
popularity?: number,
poster_path: string,
release_date: string,
title: string,
video?: boolean,
vote_average?: number
}
export async function getDataMovies(api: string) {
const resp = await fetch(api);
const data: Resp = await resp.json();
const {page, results} = data;
const listLength: number = results.length;
const movies: Object[] = [];
for await (const movie of results) {
const {title, overview, id}: Movie = movie; // bug is here
const releaseDate: Resp = movie.release_date;
const posterPath: Resp = movie.poster_path;
movies.push({
title, overview, releaseDate, posterPath, id
});
}
}
Answer the question
In order to leave comments, you need to log in
So what don't you understand? Your Object is used where a Movie is expected. Either make Resp.results of type Movie[], or don't cast the result of fetch to type Resp
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question