While refactoring some TypeScript code, I encountered an issue that has me feeling a bit stuck.
I'm curious about how the "as" keyword converts a Map<number, Trip>
into a "Trip" object in the code snippet below. If it's not doing that, then what exactly is its purpose?
public mergeBookings(bookingData: Immutable.Map) {
// Merge bookings
const bookings: Immutable.Map = this.mergeBookingData(bookingData);
// Sort bookings
const sortedBookings: Immutable.Map = SortingService.sortBookings(bookings);
// Add bookings to trip
const trip: Map<number, Trip> = this.setBookingData(sortedBookings);
return trip as Trip;
}
I'd appreciate an explanation on how the "as" keyword works with maps and other objects, along with some documentation. Any information would be helpful for maintaining this code base.
Specifically, I want to update the function definition to include the return type but need assurance that it indeed returns a "Trip" object. Below is an example of the desired function definition:
public mergeBookings(bookingData: Immutable.Map<number, Booking>): Trip {...}
I have searched through StackOverflow answers related to TypeScript and spent quite some time googling for documentation, but haven't found much help. The tags associated with this code are:
TypeScript
Angular
Immutable.js