1

I'm starting to think there is no easy solution for what I want to do. I have a class that takes in 10 parameters, most of them are optional. For simplicity reasons I will depict my issue using only 3 parameters.

I want my to be able to call the PageConfig Constructor omitting some or all of the optional parameters. e.g

new PageConfig({getList: this.getList})
new PageConfig({getList: this.getList, canDelete: false });
new PageConfig({getList: this.getList, isSliding: true });

I've tried to do this the following way.

export class PageConfigArgs {
    canDelete?: boolean;
    isSliding?: boolean;
    getList: (pagingInfo: PagingInfo) => Observable<any>;
}

export class PageConfig extends PageConfigArgs {
  constructor({
    isSliding = false,
    canDelete = true
  }: PageConfigArgs) {}
}

Since getList is required it doesn't have a default in the constructor.
but the issue is I don't know how to reference getList from the constructor to assign it to the internal one.

How can I create a new configuration class mixing optional and required parameters?

Edit My main focus is to create easy initialization which doesn't require me to configure the configuration class after initialization.

Edit Closed as duplicate, I didn't realize I needed to declare all of the properties I wanted in the config inside my class. See previous question for resolution

johnny 5
  • 19,893
  • 50
  • 121
  • 195
  • as a suggestion, why don't send a single delimited string and slice it? but you need to fix parameter positions – Mahmoud Nasr Aug 25 '19 at 14:31
  • I'm inclined to close this as a duplicate of [your other question](https://stackoverflow.com/questions/57642204/constructor-destructuring-with-exposed-parameters) unless you can explain how they differ and how the answer to the other question fails to answer this one. – jcalz Aug 25 '19 at 14:36
  • what do you mean send a string of json, and then parse it back into the object? – johnny 5 Aug 25 '19 at 14:36
  • @jcalz they differ because in the previous question, the initialization cannot skip optional parameters – johnny 5 Aug 25 '19 at 14:37
  • e.g all of the answers require an object to be copied, but there is no way to reference the destructured object into the copy, when destructuring is not used, you cannot skip optional parameters – johnny 5 Aug 25 '19 at 14:39
  • I'm not sure I entirely understand what you are trying to achieve, but is it comparable to: https://angular.io/api/common/http/HttpClient#get ? Many of the angular libs provide multiple constructors varying on inputs received, e.g. a get request can take a single url string or multiple parameters – chrismclarke Aug 25 '19 at 14:46
  • [Yes, it can skip optional parameters](https://stackblitz.com/edit/typescript-8xnb8s?file=index.ts). I'm really not sure what you mean here. – jcalz Aug 25 '19 at 14:52
  • 1
    As this is now the third attempt at asking this question, perhaps putting together a quick stackblitz to make explicity was is not working as intended would be useful. – chrismclarke Aug 25 '19 at 14:54

1 Answers1

1

I don't know how to reference getList from the constructor to assign it to the internal one.

I understand that by the "internal one" you mean the member of the class.

The problem can be solved by destructuring within the constructor's body.

Then you can assign the object property to a class member.

export class PageConfigArgs {
    canDelete?: boolean;
    isSliding?: boolean;
    getList: (pagingInfo: PagingInfo) => Observable<any>;
}

export class PageConfig extends PageConfigArgs {
  private getList: (pagingInfo: PagingInfo) => Observable<any>;

  constructor(args: PageConfigArgs) {
    const { isSliding = false, canDelete = true } = args;
    this.getList = args.getList;
  }
}
Michał Pietraszko
  • 5,666
  • 3
  • 21
  • 27