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

142
Vistas
How to fix problem when trying to type an object in typescript?

I need to type data returned from a request. One of these data returns the address object. If I type by putting "address: object", the typescript marks an error not recognizing the state and city that are inside the object. How can I fix this problem?

Code:

  import axios from "axios"
import { useEffect, useState } from "react"
import { baseURL } from "../../constants/url"
import { Container, Content, Discription, ImgContainer, ProductCard, Separator } from "./styles"

interface ProductType {
    product_name: string
    address: object
    discription: string
    image: string
}


export const Product = () => {
    const [products, setProducts] = useState<ProductType[]>([])


    useEffect(() => {
        axios.get(`${baseURL}`)
            .then(response => setProducts(response.data))
    }, [])


    return (
        <Container>
            {products.map((product) => {
                return (
                    
                    <ProductCard>
                        <ImgContainer>
                            <img src={product.image} alt="" />
                        </ImgContainer>

                        <Content>
                            <h4>
                                <strong>{product.product_name}</strong>
                            </h4>
                            <p>{product.address.state}, {product.address.city}</p>
                        </Content>

                        <Separator />

                        <Discription>
                            <p>{product.discription}!</p>
                        </Discription>
                    </ProductCard>
                )
            })}
        </Container>
    )
}
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

address: object is not precise enough. If the address object actually has city and state properties, you should note those in the type definition.

interface ProductType {
    product_name: string
    address: {
        city: string
        state: string
    }
    discription: string
    image: string
}

I'd also highly recommending fixing the discription typo, both in the front-end here and in the API - typos are a frequent source of problems in programming.

axios.get(`${baseURL}`) also simplifies to axios.get(baseURL).

You should also indicate to axios that you're expecting the response to be of type ProductType[].

axios.get<ProductType[]>(baseURL)
about 4 years ago · Juan Pablo Isaza Denunciar

0

This should do the trick for you :

interface Address {
  state: string;
  city: string;
}

interface ProductType {
  product_name: string;
  address: Address;
  discription: string;
  image: string;
}
about 4 years ago · Juan Pablo Isaza Denunciar
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