I'm trying to get the type/class from an custom object instance. This will then be used as a type parameter passed to a generic function rather than just use the 'any' type.
I've tried foo.prototype, Object.getPrototypeOf(foo), typeof foo, foo.constructor, foo.constructor.name etc but haven't yet found a way to return the objects's type/class itself. The example below gives an idea of what I want to achieve - it doesn't work here because constructor.name only returns the name of the type:
var vehicle
if (selected == 'car') {
vehicle = new Car()
} else {
vehicle = new Bike()
}
var vehicleType = vehicle.constructor.name
buildVehicle<vehicleType>(vehicle)
buildVehicle<T>(vehicle:T) {
do stuff…
}
I'm pretty new to typescript and javascript so I'm not sure this is even possible, but hopefully there is a way to do this.
Thanks for any help on this.