0

I am aware of the fact that interfaces has no real effect on data at run time in Angular2. Although I would like in some way to cast my fetched data to the correct types, and this is my solution for now:

    return this.http.get('/api/events')
        .map(res => {
            var events = <Event[]>res.json();
            for(var event of events) {
                event.time = new Date(event.time);
            }
            return events;
        })

Because the angular application requires some fields in the event (here, the time as an example) to be in the correct types, I found out this solution. It is not that smooth as I would like it to be, but I am not sure there is another way to do it either. What I want is simply to map the value of event.time in each event to a Date object.

Do I have the best solution or is there a better one out there?

rablentain
  • 6,641
  • 13
  • 50
  • 91
  • Possible duplicate of [How do I cast a JSON object to a typescript class](http://stackoverflow.com/questions/22875636/how-do-i-cast-a-json-object-to-a-typescript-class) – Günter Zöchbauer Mar 27 '16 at 17:14
  • 1
    http://stackoverflow.com/questions/35917808/how-to-get-date-object-from-json-response-in-typescript/35923990#35923990 seems to be a better fit. – Günter Zöchbauer Mar 27 '16 at 17:23

0 Answers0