For AI agents: the complete documentation index is available at https://rslint.rs/llms.txt, the full documentation bundle is available at https://rslint.rs/llms-full.txt, and this page is available as Markdown at https://rslint.rs/rules/unicorn/no-mismatched-map-key.md.
close

no-mismatched-map-key

Unreleased

Configuration

rslint.config.ts
import { defineConfig, unicornPlugin } from '@rslint/core';

export default defineConfig([
  unicornPlugin.configs.recommended,
  {
    rules: {
      'unicorn/no-mismatched-map-key': 'error',
    },
  },
]);

Rule details

Use the same key when checking a map with has() and accessing it with get() or set() in the guarded branch. A different key can indicate a copy-paste error.

The rule checks single-statement if and while branches, conditional expressions, and short-circuit expressions with the check on the left. Reassigning the checked receiver or the index that selects it suppresses reports in that branch.

Examples of incorrect code for this rule

const value = map.has(key) ? map.get(anotherKey) : fallback;

if (!map.has(key)) {
  map.set(anotherKey, value);
}

Examples of correct code for this rule

const value = map.has(key) ? map.get(key) : fallback;

if (!map.has(key)) {
  map.set(key, value);
}

Options

This rule has no options or automatic fixes.

Differences from upstream

Rslint can use generic type aliases and JSDoc annotations such as inline @type casts and array types to recognize additional non-Map receivers. Rslint skips the cache accesses below. ESLint Unicorn v79.0.0 reports them when linting without TypeScript project information.

type Cache = { has(key: string): boolean; get(key: string): number };
type Identity<T> = T;

function read(cache: Identity<Cache>) {
  if (cache.has('a')) cache.get('b');
}
const cache = /** @type {{has: (key: string) => boolean, get: (key: string) => number}} */ (
  loadCache()
);
if (cache.has('a')) cache.get('b');

Rslint also recognizes const aliases of the same object key. For a receiver such as cache[index], Rslint checks that the same binding selects the map and skips accesses after index reassignments. Destructuring assignments such as [holder.map] = maps can also invalidate the check. ESLint Unicorn v79.0.0 can report these accesses.

Original documentation