In this scenario, let's consider the following type:
export type Transformer = <T extends any[], U>(
data: T,
) => U;
Now, let's examine a function that needs to adhere to this type:
export const transform: Transformer = (
data: Result[]
): { data:Result[] } => {
if (!data) {
return { data: [] };
}
data.forEach((record:Result) => {
record.extraStuff = {
foo: 'bar'
};
});
return { data: data };
};
The error message from the compiler states:
Type '(data: Result[]) => { data: RecordMatchingSearchResult[]; }' is not assignable to type 'Transformer'.
Type '{ data: Result[]; }' is not assignable to type 'U'.
Should a generic constraint be added to U to allow for proper inference?
Another observation is the absence of explicitly defining the type, leading to confusion regarding the acceptance of generic arguments exclusively within the function.