I'm currently attempting to write a higher order function without
that will return a new object without the keys passed into the returned function.
I can't see a way to get the key in the reducer to be of keyof T
as Object.keys
returns a string[]
not Array<keyof T>
, if it did I think it would solve this. Am I missing something obvious with the types here that would solve this?
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');