I want to replicate lodash\'s _.omit
function in plain typescript. omit
should return an object with certain properties removed specified via parameter
If we limit the type of keys to string [],It works. But it does not seem to be a good idea.Keys should be string | number | symbol[];
function omit(
obj: T,
...keys: K[]
): { [k in Exclude]: T[k] } {
let ret: any = {};
Object.keys(obj)
.filter((key: K) => !keys.includes(key))
.forEach(key => {
ret[key] = obj[key];
});
return ret;
}
const result = omit({ a: 1, b: 2, c: 3 }, 'a', 'c');
// The compiler inferred result as
// {
// b: number;
// }