hammerjs/hammer.js · error · TypeError
Cannot convert undefined or null to object
Error message
Cannot convert undefined or null to object
What it means
This TypeError is thrown by the Object.assign polyfill in src/utils/assign.js when the first argument (the merge target) is undefined or null. The library intentionally replicates the spec-mandated behavior of native Object.assign, which requires a coercible object as the target. It means you called the assign/merge helper with no valid destination object, so there is nowhere to copy the source properties into.
Source
Thrown at src/utils/assign.js:13
/**
* @private
* extend object.
* means that properties in dest will be overwritten by the ones in src.
* @param {Object} target
* @param {...Object} objects_to_assign
* @returns {Object} target
*/
let assign;
if (typeof Object.assign !== 'function') {
assign = function assign(target) {
if (target === undefined || target === null) {
throw new TypeError('Cannot convert undefined or null to object');
}
let output = Object(target);
for (let index = 1; index < arguments.length; index++) {
const source = arguments[index];
if (source !== undefined && source !== null) {
for (const nextKey in source) {
if (source.hasOwnProperty(nextKey)) {
output[nextKey] = source[nextKey];
}
}
}
}
return output;
};
} else {
assign = Object.assign;
}View on GitHub (pinned to ff687ea0da)
Solutions
- Ensure the first argument is always an object: pass {} as the target when you only need a fresh merged copy (e.g. assign({}, defaults, options)).
- Default the target at the call site: assign(options || {}, overrides) so null/undefined is coerced to a new object.
- If merging into an async-fetched or lazily-created object, verify it has resolved/been initialized before calling the helper.
- Check argument order in the call; a source object must not be passed as the first (target) argument.
Example fix
// before
const merged = assign(userOptions, defaults); // userOptions is undefined
// after
const merged = assign({}, defaults, userOptions); Defensive patterns
Strategy: type-guard
Validate before calling
if (target === undefined || target === null) {
throw new Error('merge target must be a non-null object');
}
const merged = assign(target, sources); Type guard
function isMergeTarget(value) {
return value !== undefined && value !== null && (typeof value === 'object' || typeof value === 'function');
}
// usage: if (!isMergeTarget(target)) { /* bail or default to {} */ } Try / catch
let merged;
try {
merged = assign(target, source);
} catch (err) {
if (err instanceof TypeError && err.message.includes('Cannot convert undefined or null to object')) {
merged = assign({}, source); // fall back to a fresh object
} else {
throw err;
}
} Prevention
- Always pass a literal {} as the target when you want a new object instead of mutating an input.
- Default nullable inputs with target || {} before merging.
- Keep the target argument first in every call; review argument order after refactors.
- Unit-test the merge helper with null/undefined targets to catch regressions.
- Enable strict null checks / static analysis (TS strict mode, ESLint no-unsafe-optional-chaining) to catch possibly-undefined targets before runtime.
When it happens
Trigger: Calling assign(null, {a:1}), assign(undefined, src), or assign() with no arguments at all; also passing a variable that resolves to null/undefined at runtime (e.g. an unloaded config object or a failed lookup) as the first argument while sources are valid.
Common situations: Spreading a configuration object that was never initialized (const cfg; assign(cfg, defaults)); merging a state object that a loader returned null for; calling a merge utility before data fetch resolves; refactors where the target argument was accidentally dropped or reordered so a source object lands in the target slot.
AI-assisted analysis of hammerjs/hammer.js@ff687ea0da (2026-08-31).
Data as JSON: /api/errors/52602f753b53fe5e.
Report an issue: GitHub.