In my constructor, I utilize destructuring to simplify the parameters needed to create an object with default values.
export class PageConfig {
constructor({
isSliding = false
}: {
isSliding?: boolean;
getList: (pagingInfo: PagingInfo) => Observable<any>;
}) {}
}
My goal is to make the properties passed into the constructor public without redeclaring them or using an intermediary object. For example, if a class instanciates my object like this:
class UsersPage {
config = new pageConfig({ this.getList })
getList(pagingInfo: PagingInfo) {
// do work...
}
}
I want the config object to expose two things:
config.getList()
config.isSliding
What is the most efficient way to achieve this?
EDIT I've attempted to address this by creating a base class from which both the constructor arguments and the class inherit. However, if I omit the properties in the destructuring, I cannot reference them in the constructor.
For example:
export class PageConfigArgs {
isSliding?: boolean;
getList: (pagingInfo: PagingInfo) => Observable<any>;
}
export class PageConfig extends PageConfigArgs {
constructor({
isSliding = false
}: PageConfigArgs ) {
super();
this.isSliding = isSliding;
//this fails since its not declared in the destructuring
//However I do not want to declare it since it is required
this.getList = getList;
}
}