prefer-ts-extras-string-starts-with
Prefer stringStartsWith from ts-extras for direct String.prototype.startsWith calls.
Targeted pattern scopeโ
The receiver must be a primitive string. The rule checks the TypeScript declaration of .startsWith and the types of its arguments.
What this rule reportsโ
A direct .startsWith(search) call, optionally with a numeric or undefined position. Computed members, optional chains, spreads, and arguments with observable side effects are excluded.
Why this rule existsโ
Use stringStartsWith to narrow a string to a template literal starting with a known prefix.
โ Incorrectโ
declare const filename: string;
const matches = filename.startsWith("config-");
โ Correctโ
import { stringStartsWith } from "ts-extras";
declare const filename: string;
const matches = stringStartsWith(filename, "config-");
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.startsWith("config-", 1);
โ Correct โ Additional exampleโ
import { stringStartsWith } from "ts-extras";
declare const filename: string;
const result = stringStartsWith(filename, "config-", 1);
โ Correct โ Repository-wide usageโ
import { stringStartsWith } from "ts-extras";
export const matchesFilename = (filename: string) =>
stringStartsWith(filename, "config-");
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-starts-with": "warn" },
},
];
When not to use itโ
Leave this rule disabled if your project prefers direct native searches.
Package documentationโ
ts-extras package documentation:
stringStartsWith implementation and overloads
Rule catalog ID: R136