The provided function has some type definition issues in its return type. Specifically, { [K in keyof O]: O[K] extends undefined ? never : O[K] } would produce a type incompatible with the actual returned object.
Here’s the implementation
function objectWithoutUndefined<O extends Record<string, unknown>>(
obj: O,
): Partial<O> {
Object.keys(obj).forEach((key) => {
if (obj[key] === undefined) {
delete obj[key];
}
});
return obj;
}