babel-plugin-transform-object-hasown
raw JSON → 1.1.0 verified Sat Apr 25 auth: no javascript
Babel plugin for transforming Object.hasOwn() (ES2022 proposal) to a polyfill using Object.prototype.hasOwnProperty.call(). Current stable version: 1.1.0. Low release cadence, matches official polyfill behavior since v1.1.0. Key differentiator: targeted transform for the accessible Object.hasOwn proposal, only transforms exact 'Object.hasOwn' or 'Object["hasOwn"]' patterns.
Common errors
error ReferenceError: _objectHasOwn is not defined ↓
cause Plugin not configured or plugin is not transforming the code (maybe source file not processed by Babel).
fix
Ensure the plugin is added to your Babel config and the file is processed by Babel.
error TypeError: Cannot convert undefined or null to object ↓
cause Plugin's custom helper throws this error exactly as Object.hasOwn would — it's intentional, but may surprise developers expecting silent false.
fix
Guard the call: if (obj != null && Object.hasOwn(obj, 'prop')) ...
Warnings
gotcha Only transforms exact patterns Object.hasOwn or Object['hasOwn'] — not Object['hasOwn']() with computed call or module aliases. ↓
fix Always use Object.hasOwn or Object['hasOwn'] in source code.
gotcha Uses a custom _objectHasOwn helper function; not a true runtime polyfill. The helper is defined per file, not shared. ↓
fix If you need a shared polyfill, consider using @babel/polyfill or core-js.
gotcha Plugin does not check for existing runtime polyfill; if Object.hasOwn is already natively supported, transform still applies. ↓
fix Use with browserslist or @babel/preset-env to avoid unnecessary transforms.
Install
npm install babel-plugin-transform-object-hasown yarn add babel-plugin-transform-object-hasown pnpm add babel-plugin-transform-object-hasown Imports
- plugin wrong
module.exports = { plugins: ['transform-object-hasown'] };correctmodule.exports = { plugins: ['babel-plugin-transform-object-hasown'] };
Quickstart
// Install: npm install --save-dev babel-plugin-transform-object-hasown
// .babelrc
{
"plugins": ["babel-plugin-transform-object-hasown"]
}
// Input
const obj = {};
if (Object.hasOwn(obj, 'prop')) {
console.log('has prop');
}
// Output: object.hasOwn replaced with a helper that uses Object.prototype.hasOwnProperty.call