Skip to main content

prefer-ts-extras-map-has

Prefer mapHas from ts-extras for direct Map.prototype.has calls.

Targeted pattern scopeโ€‹

The receiver must be a native Map or ReadonlyMap. The rule checks the TypeScript declaration of .has and the types of its arguments.

What this rule reportsโ€‹

A direct .has(key) call with a compatible key. Computed members, optional chains, spreads, and arguments with observable side effects are excluded.

Why this rule existsโ€‹

Use mapHas to narrow a candidate key when a lookup succeeds.

โŒ Incorrectโ€‹

const statusLabels = new Map([["ready", 1]]);
const present = statusLabels.has("ready");

โœ… Correctโ€‹

import { mapHas } from "ts-extras";

const statusLabels = new Map([["ready", 1]]);
const present = mapHas(statusLabels, "ready");

Behavior and migration notesโ€‹

For a key already assignable to the declared key type, the helper returns a boolean. A broader candidate such as unknown can narrow after a successful lookup. A failed lookup can also mean a key of the right type is absent. The rule skips wider key unions when the predicate could incorrectly exclude one of their members in the false branch.

The rule requires type information and a stable ts-extras 2.x or 3.x release. It reports without applying an edit. Review code that reassigns native methods before replacing the call.

Additional examplesโ€‹

โŒ Incorrect โ€” Additional exampleโ€‹

const statusLabels = new Map([["ready", 1]]);
declare const candidate: string;
const result = statusLabels.has(candidate);

โœ… Correct โ€” Additional exampleโ€‹

import { mapHas } from "ts-extras";

const statusLabels = new Map([["ready", 1]]);
declare const candidate: string;
const result = mapHas(statusLabels, candidate);

โœ… Correct โ€” Repository-wide usageโ€‹

import { mapHas } from "ts-extras";

export const hasStatus = (labels: ReadonlyMap<string, number>, key: unknown) =>
mapHas(labels, key);

ESLint flat config exampleโ€‹

import parser from "@typescript-eslint/parser";
import typefest from "eslint-plugin-typefest";

export default [
{
files: ["**/*.ts"],
languageOptions: { parser, parserOptions: { projectService: true } },
plugins: { typefest },
rules: { "typefest/prefer-ts-extras-map-has": "warn" },
},
];

When not to use itโ€‹

Leave this rule disabled if your project prefers direct native searches.

Package documentationโ€‹

ts-extras package documentation:

mapHas implementation and overloads

Rule catalog ID: R134

Further readingโ€‹

Adoption resourcesโ€‹