I wanted to create a very simple function that takes any object and returns a new object with sorted properties, which is very simple in JavaScript:
function sortProperties(obj) {
return Object.keys(obj).sort().reduce((accum, key) => {
accum[key] = obj[key];
return accum;
}, {})
}
However, when adding types to it I'm having a lot of trouble to make the function accept any kind of object shape, throwing the following errors:
Type '{}' is not assignable to type 'T'. 'T' could be instantiated with an arbitrary type which could be unrelated to '{}'.
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 '{}'.
Ideally this should be able to work with any incoming types as it's quite generic.
I wanted to avoid using any
because it otherwise complains about unsafe any elsewhere...
Any ideas how to make it stop complaining without @ts-ignore
?