Skip to content
← Back to rules

typescript/prefer-string-starts-ends-with Style

This rule is turned on by default when type-aware linting is enabled.
💭 This rule requires type information.

What it does

Prefer startsWith and endsWith over manual string boundary checks.

Why is this bad?

Boundary checks written with slice, indexOf, regex anchors, or manual indexing are harder to read and maintain than startsWith/endsWith.

Examples

Examples of incorrect code for this rule:

ts
value.slice(0, 3) === "foo";
value.slice(-3) === "bar";

Examples of correct code for this rule:

ts
value.startsWith("foo");
value.endsWith("bar");

Configuration

This rule accepts a configuration object with the following properties:

allowSingleElementEquality

type: "always" | "never"

default: "never"

Whether equality checks against the first/last character are allowed.

"always"

Always allow equality checks against the first or last character.

"never"

Never allow equality checks against the first or last character.

How to use

To enable this rule using the config file or in the CLI, you can use:

json
{
  "options": {
    "typeAware": true
  },
  "rules": {
    "typescript/prefer-string-starts-ends-with": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  options: { typeAware: true },
  rules: {
    "typescript/prefer-string-starts-ends-with": "error",
  },
});
bash
oxlint --type-aware --deny typescript/prefer-string-starts-ends-with

Version

This rule was added in v0.0.8.

References