Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

361
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda