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

135
Visualizações
Remove undefined elements from a typescript class

I have created a javascript class so it can be reused in the project.

However, not all attributes are being set in the class at a given time.

For example, this is the interface of the address class

interface Address {
  use: Code,
  type: Code,
  text: string,
  city: string,
  district: string,
  state: string,
  postalCode: string,
  country: string,
  period: Period,
  value?: string | null,
}

class Address {
  constructor() {
  }
}

If you see the interface, you see that use has a type of Code which is another type of a class which has method called get() which will return a value or an Object

The Address class has a getObject property which will create an object which can later be strigified by using JSON.strigify()

Here is how I'm going to set the values of an object created by this class,

  const address = new Address()
  address.city = "Brisbane"
  address.country = "AUS"
  address.line.push("400, gStreet")
  address.postalCode = "4000"
  address.state = "QLD"
  address.use = new Code("work")

However, when i call address.getObject() this will trigger an error because the use is not set undefined and therefore can't call the method, get().

How can I solve this issue?

Do I have to use something like lodash to remove undefined elements within an object before calling getObject()?

Thank you in advance.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I can comment due to StackOverflow:

// so this is a comment the typescript type system is structural meaning that u can use objects when needed and object ( class instance ) when needed for example, if u want typescript to help u with types and validations use ( classes, types, and interfaces ) otherwise u can use a plain javascript object as @Aluan Haddad said

U can read more here ( not mine ) typescript type system

Good luck

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