0

I have a service generated with CLI that stores an object:

public tutorial: Object = {
    amountMin: 5,
    amountMax: 5,
    characters: 100,
    spells: 100,
    consumable: 100,
    gold: 90
};

and when I import that service into the constructor of the component like this

constructor(
    private Packs: PacksService
) {}

I can console.log(this.Packs.tutorial) and get the object I have in the service. But if I want to use this.Packs.tutorial.gold, for example, it says:

[ts] Property 'gold' does not exist on type 'Object'. any
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
zmuci
  • 518
  • 1
  • 5
  • 21
  • 2
    You've explicitly typed it as `Object` - why not create an interface to use instead? – jonrsharpe Oct 22 '17 at 09:01
  • @jonrsharpe messing with Interfaces helped me. Not sure if the implementation is 100% correct, but it solved my problem, so thank you :) – zmuci Oct 22 '17 at 09:21
  • 1
    `Object` is not what you want. That's the object constructor. Perhaps you want `object`, or nothing at all. Anyway, why did you title your question "passing object from service to component"? It seems like what you meant to ask was "accessing object in service from component", but actually your question has nothing to do with services, or components, or Angular for that matter. –  Oct 22 '17 at 12:25
  • 1
    @torazaburo is correct and, as he also hints at, you may want no type annotation at all. In this case, it would resolve all of your issues. Don't add types prematurely. Type-inference is very powerful and often gives you a better result especially while you're prototyping than if you had written the types by hand before you knew what their formalization would be. – Aluan Haddad Oct 23 '17 at 03:39

0 Answers0