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.