interface DataGeneric {
value: number;
}
function transform<D extends DataGeneric>(data: DataGeneric[], get_value: (record: D) => number) {
// No errors, works fine
let values = data.map(get_value);
// However, this line causes an error:
// Argument of type 'DataGeneric' is not assignable to parameter of type 'D'.
//
values = data.map(d => get_value(d));
// It works without error when using type assertion, but why is it needed?
values = data.map(d => get_value(d as D));
}
I'm puzzled about the necessity of type assertion when passing a single value to get_value
.
Using Typescript 2.3.4