{"record":{"id":"2abefab9946a2d6e","repo":"angular/components","slug":"found-multiple-cdkoption-representing-the-same-val","errorCode":null,"errorMessage":"Found multiple CdkOption representing the same value under the given compareWith function","messagePattern":"Found multiple CdkOption representing the same value under the given compareWith function","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"src/cdk/listbox/listbox.ts","lineNumber":994,"sourceCode":"\n  /** Verifies that no two options represent the same value under the compareWith function. */\n  private _verifyNoOptionValueCollisions() {\n    this.options.changes.pipe(startWith(this.options), takeUntil(this.destroyed)).subscribe(() => {\n      const isEqual = this.compareWith ?? Object.is;\n      for (let i = 0; i < this.options.length; i++) {\n        const option = this.options.get(i)!;\n        let duplicate: CdkOption<T> | null = null;\n        for (let j = i + 1; j < this.options.length; j++) {\n          const other = this.options.get(j)!;\n          if (isEqual(option.value, other.value)) {\n            duplicate = other;\n            break;\n          }\n        }\n        if (duplicate) {\n          // TODO(mmalerba): Link to docs about this.\n          if (this.compareWith) {\n            console.warn(\n              `Found multiple CdkOption representing the same value under the given compareWith function`,\n              {\n                option1: option.element,\n                option2: duplicate.element,\n                compareWith: this.compareWith,\n              },\n            );\n          } else {\n            console.warn(`Found multiple CdkOption with the same value`, {\n              option1: option.element,\n              option2: duplicate.element,\n            });\n          }\n          return;\n        }\n      }\n    });\n  }","sourceCodeStart":976,"sourceCodeEnd":1012,"githubUrl":"https://github.com/angular/components/blob/0411926e7d8ae06b32236ec1048a888cfad5abf2/src/cdk/listbox/listbox.ts#L976-L1012","documentation":"CdkListbox requires each option to map to a unique value under its compareWith function; otherwise selection state (which stores values, not option instances) becomes ambiguous. During listbox validation, if two options compare equal via the provided compareWith, the listbox warns with both option elements and the compareWith function.","triggerScenarios":"Two CdkOption components under a CdkListbox with [compareWith] set produce values that the compareWith function reports equal during the duplicate-check pass in CdkListbox validation.","commonSituations":"A compareWith that ignores distinguishing fields (e.g. only comparing id, but data contains duplicate ids); a compareWith that always returns true due to a bug (wrong property, reversed comparator); reusing the same value object for multiple options.","solutions":["Fix the compareWith function so it correctly distinguishes non-equal values (compare a unique key)","Ensure option values have unique identity keys in your data","Add validation upstream to de-duplicate data before rendering options"],"exampleFix":"// before\nthis.compareWith = (a, b) => a.id === b.id; // duplicate ids in data\n// after\nthis.compareWith = (a, b) => a.id === b.id && a.type === b.type; // or de-dupe data by id","handlingStrategy":"validation","validationCode":"// before rendering options\nconst seen = [];\nfor (const v of optionValues) {\n  if (seen.some(s => compareWith(s, v) === 0)) console.warn('Duplicate option value under compareWith');\n  seen.push(v);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Ensure compareWith distinguishes all distinct domain values","Include a unique key (id) in the comparator","De-duplicate source data before binding options","Unit-test compareWith against representative value pairs"],"tags":["listbox","cdk-listbox","duplicate-values","comparewith"],"backgroundTag":"duplicate-option-value","analyzedSha":"0411926e7d8ae06b32236ec1048a888cfad5abf2","analyzedAt":"2026-08-31T11:58:23.400Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}