@tienedev/datype
Version:
Modern TypeScript utility library with pragmatic typing and zero dependencies
37 lines (35 loc) • 1.2 kB
JavaScript
/**
* Creates a new object excluding the specified keys from the source object.
* Provides perfect TypeScript type inference for the resulting object.
*
* @param obj - The source object to omit from
* @param keys - Array of keys to exclude from the source object
* @returns A new object without the specified keys
*
* @example
* ```typescript
* const user = { id: 1, name: 'John', email: 'john@example.com', age: 30 };
* const publicInfo = omit(user, ['email', 'age']);
* // Result: { id: 1, name: 'John' }
* // Type: { id: number; name: string }
* ```
*/
function omit(obj, keys) {
if (typeof obj !== 'object' || obj === null) {
throw new TypeError('First argument must be an object');
}
if (!Array.isArray(keys)) {
throw new TypeError('Second argument must be an array of keys');
}
const keysToOmit = new Set(keys);
const result = {};
for (const key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key) &&
!keysToOmit.has(key)) {
// TypeScript needs explicit casting here due to the complexity of Omit<T, K>
result[key] = obj[key];
}
}
return result;
}
export { omit };