2

I need to be able to implement either static method against specific interface or extend a DTO which is implementing specific interface.

Let me try to explain a bit with code:

interface Car {
      dateOfProduction: Date

      age: (car: Car) => number
    }

I am receiving from an API:

{ dateOfProduction: Date }

Loading it like this:

let car: Car = <Car> receivedDto;

I need to call something like

Car.age(car)

or if it's possible with prototyping?

Thank you!

2
  • I don't really get the question. Would'nt you want to use a "car" class which implements your interface? Commented Jan 28, 2017 at 20:28
  • The DTO is coming from an API and have only dateOfProduction. I want to have the age either as auto calculated property or to have a helper method attached to the interface Commented Jan 28, 2017 at 20:37

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the response. The issue is if the ICar is big interface with nested properties. It become tedious to maintain. Is there "best practice" way to make deep cloning in typescript?
what do you mean bei deep cloning in this case? Or do you maybe mean reflection? If so you should look up some other questions about this topic here on SO. However i don't have a general answer for the maintainability problem with this approach.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.