typescripttypescript2.2

Typescript return value wrapped in curly braces?


In the following code block heros is wrapped with curly braces:

  export class InMemoryDataService implements InMemoryDbService {
      createDb() {
        let heroes = [
          {id: 11, name: 'Mr. Nice'},
          {id: 12, name: 'Narco'},
          ...
        ];
        return {heroes};
      }
    }

In particular reason for this?


Solution

  • Yes, you return it as an object that looks like this:

    {
        heroes: heroes
    }
    

    It's a "shortcut" to use this form: { heroes }.

    More on this here: Object initializer - New notations in ECMAScript 2015