I'm looking to restrict the field parameter within this function:
function calculate<T>(source: T[], field: keyof T) {
for(const item of source) {
}
}
The goal is to ensure that item[field]
will always be a number.
Is there a way to achieve this?
UPDATE:
In addition, how can I limit the non-generic function parameter like so:
function bar(field: keyof MyObject){ }
to prevent situations like
interface MyObject { name: string, value: number; }
bar("name"); // should result in a compiler error