Search code examples
angulartypescriptangular7

How to create object from static method which accepts function


 static fromDto(httpClient: HttpClient, adalSvc: MsAdalAngular6Service) {
        return (dto: Dto) => {
          return new Note(httpClient, dto.xxx, adalSvc);

        };
   }

How to get the Note object from this static method in typescript. I have create a Dto object and trying to pass it to this method which will give me a Note object but can't seem to do that.

What I did was this

const notes = dtos.map(d => {
Note.fromDto(null, null).apply(d); });

can you please tell me how to do that?


Solution

  • Your fromDto returns a function whose signature matches what the map array function callback. At the moment your map callback is returning nothing, so will produce an array of undefined values.

    You can use the long version and return from the callback:

    const notes = dtos.map(d => {
      return Note.fromDto(null, null)(d); 
    });
    

    Or the slightly shorter version with a single-line body:

    const notes = dtos.map(d => Note.fromDto(null, null)(d));
    

    Or you can pass the function in as the callback:

    const notes = dtos.map(Note.fromDto(null, null));
    

    DEMO: https://stackblitz.com/edit/angular-nsdpcr