What we want to solve
I would like to retrieve a value using NewsAPI, but I do not know how to set the response type. Specifically, I think I need to set the type to an array of object types, but I don't know how to do that.
Code
type
export type AllNews = {
status: string;
totalResults: number;
articles: [{
source: {
id: string;
name: string;
}
author: string;
title: string;
description: string;
url: string;
urlToImage: string;
publishedAt: string;
content: string;
}]
}
import axios from "axios"
import { AllNews } from "../../types/AllNews"
・・・
export const Top = () => {
const [ news, setNews] = useState<AllNews>()
useEffect(() => {
const getTopNews = () => {
axios.get<AllNews>('https://newsapi.org/v2/top-headlines?country=us&apiKey=xxxxxx')
.then((res) => {
setNews(res.articles)
})
}
})
・・・