Object Property Omission Utility
omit.js is a lightweight JavaScript utility function designed to create a shallow copy of an object, excluding specified fields. The current stable version is 2.0.2. This package provides a straightforward API for basic object manipulation, returning a new object without mutating the original. Its release cadence has been very slow, with the last update in 2018. Key differentiators include its minimal footprint and singular focus on shallow property omission. It is not intended for deep cloning or complex data transformations, focusing purely on filtering top-level properties. Its utility can be partially superseded by native JavaScript spread syntax for simple cases, and TypeScript's built-in `Omit` utility type for type manipulation.
Common errors
-
TypeError: omit is not a function
cause Attempting to use `import { omit } from 'omit.js'` in an ES Module context, or `const omit = require('omit.js').omit;` in a CommonJS context. The library exports its main function as a default export for ESM.fixFor ES Modules, use `import omit from 'omit.js';`. For CommonJS, use `const omit = require('omit.js');`. -
Property 'someNestedProp' of 'object' is undefined after omit operation.
cause `omit.js` only operates on top-level properties. If you omit a parent property, all its nested contents will also be removed. If you intended to omit a nested property, `omit.js` is not the right tool for that directly.fixTo omit nested properties, you need to either restructure your object before calling `omit.js` (e.g., flatten it) or use a different utility designed for deep property manipulation. For example, `omit(myObject, ['parent.nestedProp'])` will not work; `omit(myObject, ['parent'])` would remove `parent` entirely. -
Type 'Omit<MyType, 'field'>' is not assignable to type 'MyType'.
cause This error typically occurs in TypeScript when you're attempting to assign a type that has properties omitted by the `omit.js` function (or `Omit` utility type) back to a type that expects those properties to exist.fixEnsure that the type of the variable receiving the result of `omit.js` (or `Omit<Type, Keys>`) correctly reflects the properties that have been removed. You might need to define a new type with `Omit<OriginalType, 'field1' | 'field2'>` or `Partial<OriginalType>`.
Warnings
- breaking Version 2.0.0 involved significant changes to the build system (switching from `rc-tools` to `father`) and internal dependencies. While the primary API `omit(obj, fields)` remained consistent, these internal changes could lead to breaking changes in module resolution, bundling behavior, or dependency conflicts for some consumers depending on their specific build setups.
- gotcha The `omit.js` function performs a shallow copy of the object. This means that while the top-level properties are new references (or omitted), any nested objects or arrays are copied by reference, not by value. Mutating a nested object in the original will also affect the corresponding nested object in the shallow-copied result (unless that nested object was itself omitted).
- deprecated The functionality provided by `omit.js` can often be achieved with native JavaScript features like object destructuring with rest/spread syntax (`{ id, ...rest } = obj;`). For TypeScript, the built-in `Omit<Type, Keys>` utility type provides compile-time type manipulation for excluding properties, making external libraries for this specific task less necessary in many modern projects.
- gotcha The package has seen very limited maintenance since its last release (v2.0.2) in 2018. While its core functionality is simple and stable, users should be aware that it may not receive updates for new JavaScript features, bug fixes, or security patches, which could be a concern for long-term projects or those requiring active support.
Install
-
npm install omit.js -
yarn add omit.js -
pnpm add omit.js
Imports
- omit
import { omit } from 'omit.js'; // `omit.js` uses a default export for ESM const omit = require('omit.js').omit;import omit from 'omit.js';
- omit
import omit from 'omit.js'; // CommonJS environments do not support ES Modules `import` syntax
const omit = require('omit.js'); - OmitFunction
import { Omit } from 'omit.js'; // Omit is a built-in TypeScript utility type; avoid name collision.import type { OmitFunction } from 'omit.js';
Quickstart
import omit from 'omit.js';
const userData = {
id: 'user-123',
username: 'jsmith',
email: 'jsmith@example.com',
passwordHash: 'somehashedvalue',
createdAt: new Date(),
settings: { theme: 'dark', notifications: true }
};
// Use omit.js to create a new object without sensitive or unnecessary fields
const publicProfile = omit(userData, ['passwordHash', 'email', 'createdAt']);
console.log('Public Profile:', publicProfile);
// Expected: { id: 'user-123', username: 'jsmith', settings: { theme: 'dark', notifications: true } }
// The original object remains untouched
console.log('Original User Data:', userData);
const postData = {
title: 'My First Post',
content: 'Hello World!',
authorId: 'user-123',
tags: ['javascript', 'webdev']
};
// Omit a single field
const postWithoutAuthor = omit(postData, ['authorId']);
console.log('Post without Author:', postWithoutAuthor);
// Important: omit.js creates a shallow copy. Nested objects like 'settings'
// in 'publicProfile' are copied by reference. Changes to 'userData.settings'
// after calling omit will be reflected in 'publicProfile.settings'.