When working with httpclient, you can specify the type for the get call and receive a struct of that object. For example.
http.get<ProductData>("url:ressource:id").subscribe(x=> this.myObj = x)
This means that myObj will only appear to be the type of the object. If I have functions on that object, they are not callable. So, I understand that I need to create a new Object and assign the properties of the result of the request to it. My question is, can we have a function that takes a type as a Template and then returns an observable of that type with an instance?
something like this
http.get<ProductData>("url:ressource:id").makeNew<Product>()
where the result of makeNew would be an observable of type Product, so when you subscribe to it, you would get a Product.
I believe a simpler way to think about it can be seen in this example.
this.http
.get<IProduct>('https://dummyjson.com/products/1')
.pipe(
map((x) => {
var a = new ProductDisplay();
Object.assign(a, x);
return a;
})
)
.subscribe((x) => {
console.log('obj is ', x);
});
Is it possible to replace the .map portion with one function call like makeNew<Product>()