In handling responses from my Web API, I have designed an interface:
interface Person {
dateofbirth: string;
firstname: string;
lastname: string;
}
However, I prefer to work with a MomentJS
object rather than a string representation of a date-time. To achieve this, I created a class for a view model that includes a property for MomentJS
. In the constructor, I convert the date string to a MomentJS
object:
class PersonViewModel {
dateofbirth: Moment.moment;
firstname: string;
lastname: string;
constructor(dateofbirth: string, firstname: string, lastname: string) {
this.dateofbirth = moment.utc(dateofbirth);
this.firstname = firstname;
this.lastname = lastname;
}
}
Dealing with more than 10 fields in the API response often leads me to cumbersome manual processes. While I experimented with third-party tools for simplifying the instantiation of objects from JSON, I hesitate to depend on such tools for critical application components.
My query is - are there any built-in TypeScript features that could streamline this procedure, or would it be better to explore a completely different approach?