You probably want to create a model for your interface to implement your age() function.
You can't put logic in an interface. It's more of a blueprint.
So let's say you have an interface ICar which has the dateOfProduction property and a function called age which returns a string (i guess).
export interface ICar {
dateOfProduction: Date;
age(): string;
}
Then you want to create a class which implements this interface and the logic for your age function. For simplicity i don't put in the actual logic to calculate the age. you can take a look here for that.
export class Car implements ICar {
dateOfProduction: Date;
constructor(data: ICar) {
this.dateOfProduction = data.dateOfProduction;
}
age(): string {
return Utils.getYearsSince(this.dateOfProduction);
}
}
Then you probably have a service which fetches the data from your backend. There you want to create the objects for further use in your app.
Something like:
@Injectable()
export class CarService {
constructor(private _backendService: BackendService) {
}
getMyCar(): Observable<Car> {
// we call a backendService or directly use `http` here ...
this._backendService.get('/api/mycar/').subscribe(response: ICar) {
// so our response is in the form of our car interface,
// we can just use it in our constructor then, create an instance
// and do something with it, like get the age
let myCar: Car = new Car(response);
console.log(myCar.age());
return car;
}
}
}
Hope this is what you were looking for.