Currently, I'm working on creating a higher-order function called without
that will generate a new object without the specified keys.
I am struggling with getting the keys in the reducer to be of type keyof T
as Object.keys
returns an array of string[]
instead of Array<keyof T>
. If it returned the latter, I believe it would solve the issue. Is there something obvious regarding types that I am missing here for a solution?
const without = <T>(object: T) => (...parts: Array<keyof T>) => {
return Object.keys(object).reduce((acc, key) => {
// `key` is of type `string`, not `keyof T`
if (!parts.includes(key)) {
acc[key] = object[key];
}
return acc;
}, {} as Omit<T, typeof parts[number]>);
};
const obj = { a: 1, b: 2, c: 3 };
const result = without(obj)('a', 'c');