I need to alias a property name of a returning object and specify its type.
While I was able to successfully alias the property name, defining its type is proving to be a challenge.
NOTE: I am aware that I can cast it in my function body like this (<Car>car).id
, but I am exploring if it can be defined in the parameters declaration.
NOTE: The function dropModel
belongs to an imported package and not created by me.
To better illustrate my issue, please refer to this StackBlitz demo. In the app.component.ts
file on line 33, notice that my item is currently assigned as any
. How can I define it as belonging to the Vemp
class?
My code:
this.myService.dropModel('cars').subscribe(({ target: team, item: car }) => {
const targetTeamId = team.id;
const droppedCarId = car.id;
})
By aliasing target
as team
and item
as car
, my code has improved readability. However, the issue lies in the lack of type safety as team
and car
are both set as any
.
Definition of dropModel Function:
dropModel: (groupName?: string) => Observable<{
target: any;
item: any;
}>;