I am working on a function that can take either a single value or an array of values, and produce new values based on the input. However, the function is unable to determine whether to return a single value or an array based on the type of the input.
How can I make TypeScript understand that the current value being passed to the function is an array, and that the returned values will also be in an array format?
prepareForResponseMany2(data: ProductEntity | ProductEntity[]): ProductType | ProductType[] {
if (Array.isArray(data)) {
return data.map(entity => this.prepareForResponse(entity));
}
return this.prepareForResponse(data);
}