0

I am trying to implement a typescript class parentData with a constructor which in turn has a couple of other classes with constructors. When I do this it throws improper implementation. Not understanding what could be the reason.

export class parentData implements foodsData, places {
    constructor(
        name: string,
        id: number,
        foods: foodsData[],
        places: places
    ){}
}

export class foodsData {
    constructor(
        food1: string,
        food2: string,
        food3: string
    ){}
}

export class places {
    constructor(
        place1: string,
        place2: string,
        place3: string
    ){}
}
wazz
  • 4,953
  • 5
  • 20
  • 34

3 Answers3

2

You seem to be misunderstanding what a class is, as well as what inheritance and interfaces are. Look more into Object Oriented Programming concepts for how to design classes & interfaces and why/where/how you should use them - then what you're trying to do above might be clearer.

You have:

export class parentData implements foodsData, places{ ...}

foodsData and places are defined as classes, so you cannot implement them. You implement an interface (usually to add behavior), not a class. You extend a class (to inherit properties & methods in a subclass), but you don't need to do that here either.

Currently, foodsData and places don't need to be classes at all. All they are is limited collections of strings w/no behavior or other properties. In parentData you already have unlimited collections for foods and places so these other classes serve no real purpose.

Consider what characteristics a food or place has (properties) and consider what they can do (methods ... not so applicable in this case), and put that info into the class definitions. Then, in ParentData you can simply create arrays of type Food[] and Place[] similar to what you already have. No need to implement or extend anything. The ParentData class just has to be able to "see" the definitions for the other classes (either the classes all need to be defined in the same file, or you can import the ones you need from other files).

Also note: standard practice is to capitalize your class names, and use lowercase for an instance of that class

Example:

export class ParentData {
    constructor(name: string,
    id: number,
    foods: Food[],
    places: Place[]
    ){ }
}

export class Food {
    constructor(name: string, color: string, price: number, calories: number ){}
}

export class Place {
    constructor(name: string, address: string, zipCode: string, latitude: number, longitude: number){}
}
mc01
  • 3,750
  • 19
  • 24
1

I think these other answers are wrong. In other languages, you would certainly implement an interface and inherit a class. In Typescript, you can implement a class. This requires that all the properties and methods be implemented in the resulting type.

Your example did not cause issues for me. What I think may have been the issue is that one of your classes you are implementing has a method accessor on a constructor parameter, which means that is a property on the class. If you implement this same property on your parentData class, you should be ok.

To be clear, your example works for me:

export class parentData implements foodsData, places {
    constructor(name: string,
    id: number,
    foods: foodsData[],
    places: places[]
    ){ }
}



export class foodsData {
    constructor(food1: string,
    food2: string,
    food3: string){}
}

export class places {
    constructor(place1: string,
    place2: string,
    place3: string){}
}

This would not, since foodsData has an accessor on the constructor parameter

export class parentData implements foodsData, places {
    constructor(name: string,
    id: number,
    foods: foodsData[],
    places: places[]
    ){ }
}    


export class foodsData {
    // NOTE THE 'private' modifier below. if this were public, it would work if you also add a 'public food1: string;' to the parentData class;
    constructor(private food1: string,
    food2: string,
    food3: string){}
}

export class places {
    constructor(place1: string,
    place2: string,
    place3: string){}
}

Avoid anything other than the public modifier if you want to implement a class rather than extend it.

To everyone else: Interfaces don't hang around in Typescript; they're lost at compilation. Sometimes it's nice to have a class that stays around (to be able to use instanceof, e.g.), and implementing that class is similar to how languages like C# allow you to implement an interface.

ps2goat
  • 8,067
  • 1
  • 35
  • 68
  • Thank you so much!! Quick doubt so here is what the issue I am facing is.. in Component When I use it like new ParentData( name: bhachi, id: 2, foods: some array, places: some array) ... I am facing expected 4 but 8. Not knowing why.. – Bhachi Kumar May 09 '18 at 03:46
  • `expected 4 but 8` doesn't sound like a real message. Can you update your question with the *exact* message and your sample code that causes this issue? It seems like you aren't actually passing in arrays, but perhaps multiple items. I can't really tell without seeing the actual message and your sample code. – ps2goat May 09 '18 at 17:08
  • Actually, used the same code which you posted above, when using that class in component such a way. return new parentData({ id: 2, name: 'bhachi', foods : [ food1 : 'pizza', food2 : 'burger'], places : [place1 : 'ny', place2 : 'calif']}) When I use this way it is saying error on parentData that expected parameters 4 but got 8. – Bhachi Kumar May 09 '18 at 19:51
  • using your code, I get `Expected 4 arguments, but got 1.`. Take the curly braces off the parameters-- you're passing in an object, not an arg list like it expects. – ps2goat May 09 '18 at 22:26
  • I did get the `expected 4, got 8` after I cleaned that up. You also need to clean up the property names and colons. Here's what I ended up with that works: `return new parentData('bhachi', 2, [ 'pizza', 'burger'], ['ny','calif']);` – ps2goat May 09 '18 at 22:28
0

You can only implement an interface. For a normal or abstract class, you have to extend it.

Phil
  • 7,065
  • 8
  • 49
  • 91