{"record":{"id":"3d32ba0139f18ee5","repo":"immutable-js/immutable-js","slug":"cannot-update-immutable-value-without-set-metho","errorCode":null,"errorMessage":"Cannot update immutable value without .set() method: {}","messagePattern":"Cannot update immutable value without \\.set\\(\\) method: (.+?)","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/functional/set.ts","lineNumber":54,"sourceCode":"): C;\nexport function set<K, V, C extends Collection<K, V> | { [key: string]: V }>(\n  collection: C,\n  key: K | string,\n  value: V\n): C {\n  if (isProtoKey(key)) {\n    return collection;\n  }\n\n  if (!isDataStructure(collection)) {\n    throw new TypeError(\n      'Cannot update non-data-structure value: ' + collection\n    );\n  }\n  if (isImmutable(collection)) {\n    // @ts-expect-error weird \"set\" here,\n    if (!collection.set) {\n      throw new TypeError(\n        'Cannot update immutable value without .set() method: ' + collection\n      );\n    }\n    // @ts-expect-error weird \"set\" here,\n    return collection.set(key, value);\n  }\n  // @ts-expect-error mix of key and string here. Probably need a more fine type here\n  if (hasOwnProperty.call(collection, key) && value === collection[key]) {\n    return collection;\n  }\n  const collectionCopy = shallowCopy(collection);\n  // @ts-expect-error mix of key and string here. Probably need a more fine type here\n  collectionCopy[key] = value;\n  return collectionCopy;\n}\n","sourceCodeStart":36,"sourceCodeEnd":70,"githubUrl":"https://github.com/immutable-js/immutable-js/blob/59fdaae67606ceedee7902a44703c893949f673b/src/functional/set.ts#L36-L70","documentation":"updateIn walks a keyPath level by level. At some level the existing value exists (wasNotSet is false) but is not a data structure — you cannot index into a number/string/boolean to continue the path. The message includes the path prefix that failed.","triggerScenarios":"Map({ a: 5 }).updateIn(['a','b'], () => 1); state.updateIn(['user','name','first'], f) where user.name is a string; List path hitting a scalar element.","commonSituations":"State shape drift: a field that used to be a nested object became a scalar after a refactor or API change; key paths built dynamically with an extra segment; optional fields that default to '' or 0 instead of Maps.","solutions":["Fix the keyPath to stop at, or go around, the scalar value","Ensure intermediate values are Maps/Objects: initialize defaults as Map()/{} not scalars","Use updateIn's notSetValue argument or a custom updater that replaces non-structures: updateIn(keys, v => isDataStructure(v) ? v : Map(), then set)"],"exampleFix":"// before\nMap({ user: { name: 'Ada' } }).updateIn(['user', 'name', 'first'], fn); // name is a string\n// after\nMap({ user: { name: { first: 'Ada' } } }).updateIn(['user', 'name', 'first'], fn);","handlingStrategy":"type-guard","validationCode":"import { isDataStructure } from 'immutable/dist/predicates';\n// or: const isDS = v => isImmutable(v) || Array.isArray(v) || (v && typeof v === 'object');\nfunction pathTraversable(state, keys) {\n  let cur = state;\n  for (const k of keys.slice(0, -1)) {\n    if (cur == null) return true; // will be created\n    if (!isDS(cur)) return false;\n    cur = cur.get ? cur.get(k) : cur[k];\n  }\n  return true;\n}","typeGuard":"const isDataStructure = (v: unknown): v is object => isImmutable(v as any) || Array.isArray(v) || (!!v && typeof v === 'object');","tryCatchPattern":"try { state.updateIn(path, fn); } catch (e) { if (/non-data-structure/.test(e.message)) { /* normalize leaf to Map and retry once */ } else throw e; }","preventionTips":["Initialize nested optional fields as Map()/{} instead of '' / 0 / false","Keep keyPath arrays in one place, typed against the state schema","Migrate persisted state to the current shape before updateIn"],"tags":["updatein","keypath","type-error"],"backgroundTag":"invalid-update-target","analyzedSha":"59fdaae67606ceedee7902a44703c893949f673b","analyzedAt":"2026-08-27T20:14:53.132Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}