{"id":14783,"library":"omit.js","title":"Object Property Omission Utility","description":"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.","status":"maintenance","version":"2.0.2","language":"javascript","source_language":"en","source_url":"https://github.com/benjycui/omit.js","tags":["javascript","object","omit","typescript"],"install":[{"cmd":"npm install omit.js","lang":"bash","label":"npm"},{"cmd":"yarn add omit.js","lang":"bash","label":"yarn"},{"cmd":"pnpm add omit.js","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"For ESM, `omit.js` provides a default export. Ensure your build configuration supports importing from `.js` files without explicit extensions if not using a bundler.","wrong":"import { omit } from 'omit.js'; // `omit.js` uses a default export for ESM\nconst omit = require('omit.js').omit;","symbol":"omit","correct":"import omit from 'omit.js';"},{"note":"This is the standard CommonJS `require` syntax as demonstrated in the package's README.","wrong":"import omit from 'omit.js'; // CommonJS environments do not support ES Modules `import` syntax","symbol":"omit","correct":"const omit = require('omit.js');"},{"note":"When using TypeScript, if you need to type the `omit` function itself, look for an exported function type (e.g., `OmitFunction` or similar) to avoid conflicts with TypeScript's intrinsic `Omit<Type, Keys>` utility type.","wrong":"import { Omit } from 'omit.js'; // Omit is a built-in TypeScript utility type; avoid name collision.","symbol":"OmitFunction","correct":"import type { OmitFunction } from 'omit.js';"}],"quickstart":{"code":"import omit from 'omit.js';\n\nconst userData = {\n  id: 'user-123',\n  username: 'jsmith',\n  email: 'jsmith@example.com',\n  passwordHash: 'somehashedvalue',\n  createdAt: new Date(),\n  settings: { theme: 'dark', notifications: true }\n};\n\n// Use omit.js to create a new object without sensitive or unnecessary fields\nconst publicProfile = omit(userData, ['passwordHash', 'email', 'createdAt']);\n\nconsole.log('Public Profile:', publicProfile);\n// Expected: { id: 'user-123', username: 'jsmith', settings: { theme: 'dark', notifications: true } }\n\n// The original object remains untouched\nconsole.log('Original User Data:', userData);\n\nconst postData = {\n  title: 'My First Post',\n  content: 'Hello World!',\n  authorId: 'user-123',\n  tags: ['javascript', 'webdev']\n};\n\n// Omit a single field\nconst postWithoutAuthor = omit(postData, ['authorId']);\nconsole.log('Post without Author:', postWithoutAuthor);\n\n// Important: omit.js creates a shallow copy. Nested objects like 'settings'\n// in 'publicProfile' are copied by reference. Changes to 'userData.settings'\n// after calling omit will be reflected in 'publicProfile.settings'.","lang":"typescript","description":"Demonstrates how to import and use `omit.js` to filter properties from an object, creating a shallow copy. It shows omitting multiple and single fields, and highlights the non-mutating behavior on the top-level object, while also noting the shallow copy aspect for nested objects."},"warnings":[{"fix":"Review your module resolution settings, especially if you encountered issues upgrading from `1.x.x` to `2.x.x`. Ensure your bundler (Webpack, Rollup, etc.) or Node.js environment is configured to correctly resolve ES Modules if you are using `import` syntax.","message":"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.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"If you require a deep copy, you must implement your own deep cloning logic (e.g., using `structuredClone` in modern environments, or a dedicated deep-cloning library like `lodash.clonedeep`) before or after using `omit.js`.","message":"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).","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Consider refactoring to use native JavaScript for runtime omission (`const { excludedProp, ...rest } = myObject;`) and TypeScript's `Omit` utility type (`type NewType = Omit<OriginalType, 'prop1' | 'prop2'>;`) for compile-time type transformation. This can reduce dependency count and leverage language features.","message":"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.","severity":"deprecated","affected_versions":">=1.0.0"},{"fix":"Evaluate if the benefits of a lightweight utility outweigh the risks of a minimally maintained package. For critical applications, consider using actively maintained alternatives or implementing the omission logic natively.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"For ES Modules, use `import omit from 'omit.js';`. For CommonJS, use `const omit = require('omit.js');`.","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.","error":"TypeError: omit is not a function"},{"fix":"To 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.","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.","error":"Property 'someNestedProp' of 'object' is undefined after omit operation."},{"fix":"Ensure 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>`.","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.","error":"Type 'Omit<MyType, 'field'>' is not assignable to type 'MyType'."}],"ecosystem":"npm"}