Reputation: 45
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
){}
}
Upvotes: 0
Views: 513
Reputation: 3780
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){}
}
Upvotes: 2
Reputation: 8485
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.
Upvotes: 1
Reputation: 7607
You can only implement an interface. For a normal or abstract class, you have to extend it.
Upvotes: 0