Skip to main content

prefer-ts-extras-string-ends-with

Prefer stringEndsWith from ts-extras for direct String.prototype.endsWith calls.

Targeted pattern scopeโ€‹

The receiver must be a primitive string. The rule checks the TypeScript declaration of .endsWith and the types of its arguments.

What this rule reportsโ€‹

A direct .endsWith(search) call, optionally with a numeric or undefined endPosition. Computed members, optional chains, spreads, and arguments with observable side effects are excluded.

Why this rule existsโ€‹

Use stringEndsWith to narrow a string to a template literal ending with a known suffix.

โŒ Incorrectโ€‹

declare const filename: string;
const matches = filename.endsWith(".json");

โœ… Correctโ€‹

import { stringEndsWith } from "ts-extras";

declare const filename: string;
const matches = stringEndsWith(filename, ".json");

Behavior and migration notesโ€‹

Passing a single literal search string can narrow the receiver in the true branch. A dynamic search string or an explicit position selects the boolean overload.

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โ€‹

declare const filename: string;
const result = filename.endsWith(".json", 1);

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

import { stringEndsWith } from "ts-extras";

declare const filename: string;
const result = stringEndsWith(filename, ".json", 1);

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

import { stringEndsWith } from "ts-extras";

export const matchesFilename = (filename: string) =>
stringEndsWith(filename, ".json");

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-string-ends-with": "warn" },
},
];

When not to use itโ€‹

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

Package documentationโ€‹

ts-extras package documentation:

stringEndsWith implementation and overloads

Rule catalog ID: R135

Further readingโ€‹

Adoption resourcesโ€‹