Imagine I have a data object and I want to create a versatile mapper function that can be used for all types without having to instantiate a new class each time, like this:
this.responseMapper.map<CommentDTO>(data);
The goal is to take the properties from the specified type and map the data accordingly. Here's what I've attempted:
public map<T>(values: any): T {
const instance = new T();
return Object.keys(instance).reduce((acc, key) => {
acc[key] = values[key];
return acc;
}, {}) as T;
}
However, using new T();
results in an error:
'T' only refers to a type, but is being used as a value here.
What would be the correct approach to achieve this?