I'm trying to selectively copy key-value pairs from one object to another, so I can selectively remove properties of an object, like so:
sanitizeBody: function (obj: object, ...allowed: string[]) {
const sanitizedObject = {};
Object.keys(obj).forEach((el: string) => {
if (allowed.includes(el)) {
sanitizedObject[el] = obj[el];
}
});
}
Basically, the point of the function is that it takes an object, i.e. req.body, and a list of strings, and performs this operation. For example, an example input may look like: sanitizeBody(req.body, 'role', 'active', 'status').
This worked in Javascript,however, in TypeScript I keep getting the error:
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{}'.
No index signature with a parameter of type 'string' was found on type '{}'.
I'm not sure how to fix this.