discordjs/discord.js · error · RangeError

Expected ${prop} to be a function, received ${typeof descrip

Error message

Expected ${prop} to be a function, received ${typeof descriptor.value} instead.

What it means

The Mixin utility merges classes/objects; when it encounters the special optimization-data property (OptimizeDataPropertyName) it requires its value to be a function, and throws this RangeError otherwise.

Source

Thrown at packages/structures/src/Mixin.ts:84

			}

			if (prototype[kMixinToJSON]) {
				enrichToJSONs.push(prototype[kMixinToJSON]);
			}

			// Copy instance methods and setters / getters
			const originalDescriptors = Object.getOwnPropertyDescriptors(prototype);
			const usingDescriptors: { [prop: string]: PropertyDescriptor } = {};
			for (const [prop, descriptor] of Object.entries(originalDescriptors)) {
				// Drop constructor
				if (['constructor'].includes(prop)) {
					continue;
				}

				// Special case for optimize function, we want to combine these
				if (prop === OptimizeDataPropertyName) {
					if (typeof descriptor.value !== 'function')
						throw new RangeError(`Expected ${prop} to be a function, received ${typeof descriptor.value} instead.`);
					dataOptimizations.push(descriptor.value);
					continue;
				}

				// Shouldn't be anything other than these without being instantiated, but just in case
				if (
					typeof descriptor.get !== 'undefined' ||
					typeof descriptor.set !== 'undefined' ||
					typeof descriptor.value === 'function'
				) {
					usingDescriptors[prop] = descriptor;
				}
			}

			Object.defineProperties(destination.prototype, usingDescriptors);
		}
	}

View on GitHub (pinned to a81ed8a306)

Solutions

  1. Ensure the optimization property is defined as a function (e.g. a static resolver method)
  2. Remove the property if you did not intend to attach optimization data
  3. Check which mixin source class carries the malformed property and fix its definition

Example fix

// before
class Foo { static [OptimizeDataPropertyName] = ['a', 'b']; }
// after
class Foo { static [OptimizeDataPropertyName] = () => ['a', 'b']; }
Defensive patterns

Strategy: validation

Validate before calling

if (OptimizeDataPropertyName in MixedClass && typeof MixedClass[OptimizeDataPropertyName] !== 'function') {
  throw new Error('optimization data property must be a function before Mixin()');
}

Type guard

function hasFunctionOptimizeData(cls: unknown): boolean {
  return typeof cls !== 'function' || !(OptimizeDataPropertyName in cls) || typeof (cls as any)[OptimizeDataPropertyName] === 'function';
}

Try / catch

try {
  const Mixed = Mixin(Base, Feature);
} catch (err) {
  if (err instanceof RangeError && err.message.includes('to be a function')) {
    // inspect the optimization-data property on the inputs
  }
}

Prevention

When it happens

Trigger: Applying Mixin to a class or object whose optimization data property (e.g. [OptimizeDataPropertyName] / special resolve-style property) is set to a non-function value like an array or object.

Common situations: Manually copying static properties between classes, using a mixin pattern that stores optimization data as data rather than a function, or upgrading @discordjs/structures-adjacent packages with mismatched shapes.

Understand the failure class

Background: Invalid argument type errors: "must be of type string", "expected X, got Y", and ERR_INVALID_ARG_TYPE explained — this error's family across 15 libraries.


AI-assisted analysis of discordjs/discord.js@a81ed8a306 (2026-08-30). Data as JSON: /api/errors/deabce8624acf728. Report an issue: GitHub.