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.
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