When creating a class, I like to include a constructor that can accept any relevant members for the class:
class AnotherClass {
itemA: string;
itemB: number;
constructor(obj: Partial<AnotherClass>) {
Object.keys(obj).forEach(key => this[key] = obj[key]);
}
}
This way, I have the flexibility to instantiate objects using
new SomeObject({itemA: 'example'})
, new SomeObject({itemB: 5})
, or a combination of properties.
(In this scenario, let's assume it's a casual project and we're disregarding any reasons against formatting the constructor in this manner.)
While this approach is useful, it can clutter Intellisense with all available methods in addition to properties (even though my example doesn't contain methods).
Is there a variation or condition for Partial<AnotherClass>
that restricts it to only properties? Since I tend to use arrow functions frequently, I suppose these would be non-function properties (given that arrow functions are technically considered properties rather than methods).