JavaScript Accessor Descriptor Checker
The `is-accessor-descriptor` package provides a focused utility function to determine if a given JavaScript value, or a property on an object, represents a valid accessor property descriptor. It specifically checks for the presence of `get` and/or `set` properties and ensures they are functions, while rejecting `value` or `writable` properties which characterize data descriptors. Currently at version 3.0.5, this package is part of a family of descriptor-checking utilities under the `inspect-js` scope, emphasizing foundational JavaScript introspection. Its release cadence is stable and infrequent, reflecting its role as a fundamental helper. It differentiates itself by its precise focus, allowing developers to isolate and validate accessor descriptors without conflating them with other descriptor types, making it a reliable tool for metaprogramming and object property manipulation.
Common errors
-
TypeError: isAccessorDescriptor is not a function
cause This error most commonly occurs when attempting to call `isAccessorDescriptor` after incorrectly importing it as a named export in an ES Module environment (e.g., `import { isAccessorDescriptor } from 'is-accessor-descriptor';`). Since the package provides a default CJS export, the named import fails to resolve the function.fixAdjust your import statement to `import isAccessorDescriptor from 'is-accessor-descriptor';` if in ESM, or `const isAccessorDescriptor = require('is-accessor-descriptor');` if in CJS. -
ReferenceError: require is not defined
cause This error indicates that you are attempting to use the CommonJS `require()` function within an ES Module file. ES Modules do not inherently expose `require`.fixIf your file is an ES Module (e.g., `.mjs` or `"type": "module"` in `package.json`), you must use the ESM import syntax: `import isAccessorDescriptor from 'is-accessor-descriptor';`.
Warnings
- gotcha This package is implemented as a CommonJS module. When consuming it in an ES Module (ESM) environment, it must be imported as a default export using `import isAccessorDescriptor from 'is-accessor-descriptor';`. Attempting to use named imports (e.g., `import { isAccessorDescriptor } from 'is-accessor-descriptor';`) will typically result in `isAccessorDescriptor` being `undefined` or a runtime error in strict ESM contexts.
Install
-
npm install is-accessor-descriptor -
yarn add is-accessor-descriptor -
pnpm add is-accessor-descriptor
Imports
- isAccessorDescriptor
import { isAccessorDescriptor } from 'is-accessor-descriptor';const isAccessorDescriptor = require('is-accessor-descriptor'); - isAccessorDescriptor (ESM Interop)
import { isAccessorDescriptor } from 'is-accessor-descriptor';import isAccessorDescriptor from 'is-accessor-descriptor';
Quickstart
const isAccessorDescriptor = require('is-accessor-descriptor');
const assert = require('assert');
const obj = {
// An accessor property
get foo() { return 'bar'; },
// A data property containing an object with a 'get' key, not an accessor descriptor
bar: { get: function() { return 'baz'; } }
};
// Check a property by its descriptor on an object
assert.equal(true, isAccessorDescriptor(obj, 'foo'), 'obj.foo should be an accessor descriptor');
assert.equal(false, isAccessorDescriptor(obj, 'bar'), 'obj.bar should NOT be an accessor descriptor');
// Alternatively, if you already have the descriptor object
const fooDescriptor = Object.getOwnPropertyDescriptor(obj, 'foo');
assert.equal(true, isAccessorDescriptor(fooDescriptor), 'fooDescriptor should be an accessor descriptor');
const barDescriptor = Object.getOwnPropertyDescriptor(obj, 'bar');
assert.equal(false, isAccessorDescriptor(barDescriptor), 'barDescriptor should NOT be an accessor descriptor');
console.log('All assertions passed: is-accessor-descriptor works as expected!');