I want to replicate lodash\'s _.omit
function in plain typescript. omit
should return an object with certain properties removed specified via parameter
The accepted answer from Nurbol above is probably the more typed version, but here is what I am doing in my utils-min
.
It uses the typescript built-in Omit and is designed to only support string key names. (still need to loosen up the Set to Set, but everything else seems to work nicely)
export function omit>(obj: T, ...keys: K[]): Omit {
let ret: any = {};
const excludeSet: Set = new Set(keys);
// TS-NOTE: Set makes the obj[key] type check fail. So, loosing typing here.
for (let key in obj) {
if (!excludeSet.has(key)) {
ret[key] = obj[key];
}
}
return ret;
}