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

136
Views
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 answers
Answer question

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 Report
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!