Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

362
Views
How to declare json data fields that I am getting in an api in ReactJs

enter image description here

As can be seen from the image, I am getting error lines under element.url etc. from line no. 62. I am fetching data from news api and in result I am getting json response which looks like this. I am passing these title, description etc. from api response to another component in my react app as props.

enter image description here

Now the error says this :

any | Property 'title' does not exist on type 'never'.ts(2339)

So my question is; is there a way I can declare the rest of the fields of the json data so my editor doesn't give this error? Here is my code....

constructor(props) {
    super(props);
    this.state = {
        articles: [],
        totalResults: 0,
        loading: false,
        page: 1,
    };
}

async componentDidMount() {
    let apiKey = `https://newsapi.org/v2/top-headlines?country=in&apiKey=7e87c89bfbbd4b8b8bb0721d45a6d454&pageSize=18&page=${this.state.page}`;
    let data = await (await fetch(apiKey)).json();
    this.setState({ articles: data.articles, totalResults: data.totalResults });
}

render() {
    return (
        <div className="container my-4">
            <h2 id="heading">Top headlines of the day</h2>
            <div className="row my-4">
                {this.state.articles.map((element) => {
                    return (
                        <div className="col-md-4 mb-3" key={element.url}>
                            <NewsItem
                                title={element.title}
                                description={element.description}
                                imageUrl={element.urlToImage}
                                more={element.url}
                            />
                        </div>
                    );
                })}
            </div>
about 4 years ago · Santiago Gelvez
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!