I have interface CommunityProps and class Community implements CommunityProps. I would like to do something like this:
class Community implements CommunityProps {
// property declarations
constructor(props: CommunityProps) {
Object.assign(this, props)
}
}
But Object.assign is not typesafe. I am stuck listing out all 10-15 properties in 1) the interface, 2) the class property declarations, and 3) one-by-one assigning the class properties to props.propName where props is passed into the constructor, like this:
interface CommunityProps {
prop1: string
prop2: number
prop3: boolean
}
class Community implements CommunityProps {
prop1: string
prop2: number
prop3: boolean
constructor(props: CommunityProps) {
this.prop1 = props.prop1
this.prop2 = props.prop2
this.prop3 = props.prop3
}
}
I have about 10 such classes I need to make, each with a different set of properties. This will leave me with about 450 lines of code just to get these classes working. Surely there is a better way to do what I am trying to do?
The best alternative I have found is giving the class a single property which is of the same type as the interface, and remove the implements CommunityProps from the class declaration:
class Community {
info: CommunityProps
constructor(props: CommunityProps) {
this.info = props
}
}
But I would really, really like to be able to access these properties without having to use .info.prop1