{"record":{"id":"52602f753b53fe5e","repo":"hammerjs/hammer.js","slug":"cannot-convert-undefined-or-null-to-object","errorCode":null,"errorMessage":"Cannot convert undefined or null to object","messagePattern":"Cannot convert undefined or null to object","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/utils/assign.js","lineNumber":13,"sourceCode":"/**\n * @private\n * extend object.\n * means that properties in dest will be overwritten by the ones in src.\n * @param {Object} target\n * @param {...Object} objects_to_assign\n * @returns {Object} target\n */\nlet assign;\nif (typeof Object.assign !== 'function') {\n  assign = function assign(target) {\n    if (target === undefined || target === null) {\n      throw new TypeError('Cannot convert undefined or null to object');\n    }\n\n    let output = Object(target);\n    for (let index = 1; index < arguments.length; index++) {\n      const source = arguments[index];\n      if (source !== undefined && source !== null) {\n        for (const nextKey in source) {\n          if (source.hasOwnProperty(nextKey)) {\n            output[nextKey] = source[nextKey];\n          }\n        }\n      }\n    }\n    return output;\n  };\n} else {\n  assign = Object.assign;\n}","sourceCodeStart":1,"sourceCodeEnd":31,"githubUrl":"https://github.com/hammerjs/hammer.js/blob/ff687ea0daa3c806b9accd2ecb1a46165ea3c00a/src/utils/assign.js#L1-L31","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nconst merged = assign(userOptions, defaults); // userOptions is undefined\n\n// after\nconst merged = assign({}, defaults, userOptions);","handlingStrategy":"type-guard","validationCode":"if (target === undefined || target === null) {\n  throw new Error('merge target must be a non-null object');\n}\nconst merged = assign(target, sources);","typeGuard":"function isMergeTarget(value) {\n  return value !== undefined && value !== null && (typeof value === 'object' || typeof value === 'function');\n}\n// usage: if (!isMergeTarget(target)) { /* bail or default to {} */ }","tryCatchPattern":"let merged;\ntry {\n  merged = assign(target, source);\n} catch (err) {\n  if (err instanceof TypeError && err.message.includes('Cannot convert undefined or null to object')) {\n    merged = assign({}, source); // fall back to a fresh object\n  } else {\n    throw err;\n  }\n}","preventionTips":["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."],"tags":["typeerror","object-assign","null-target","undefined"],"backgroundTag":"cannot-convert-undefined-or-null-to-object","analyzedSha":"ff687ea0daa3c806b9accd2ecb1a46165ea3c00a","analyzedAt":"2026-08-31T14:43:49.159Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}