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?

2

0

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.