Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

143
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda