I encountered this issue with a function I have:
const fieldsOrder = [
'boo',
];
fieldsOrder.reduce((sortedRequest, key) => {
if (key in request)
sortedRequest[key] = request[key]; // Causing warnings here
return sortedRequest;
}
Both sortedRequest
and request
are objects, which leads to the warning about using index in objects. Although it works fine in plain JavaScript, I need a solution to handle and modify the code to avoid triggering that warning.
I am unable to define sortedRequest
or request
as any
or any type that uses any
, as Elinst will throw an error (and I cannot change that rule).
EDIT:
Following tscpp's advice, I modified my code as follows:
export default function sortFields (
request: Record<string, unknown>
): Partial<FeeLookupRequest> {
return fieldsOrder.reduce((sortedRequest: Record<string, unknown>, key) => {
if (key in request) {
sortedRequest[key] = request[key];
}
return sortedRequest;
}, {});
}
As a result, the warnings have been resolved.