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-unnecessary-string-trim.md.
close

no-unnecessary-string-trim

Unreleased

Configuration

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

export default defineConfig([
  unicornPlugin.configs.recommended,
  {
    rules: {
      'unicorn/no-unnecessary-string-trim': 'error',
    },
  },
]);

Rule details

This rule replaces trim() with trimStart() before startsWith() and with trimEnd() before endsWith() when trimming the other side cannot affect the result.

A second argument can make the other side of the string observable. So can trailing whitespace in a prefix or leading whitespace in a suffix. The rule also skips optional chaining on the call chain, trim() calls with arguments, and search values whose primitive string form cannot be determined statically. It skips trim() on schemas created with a string constructor from zod, including schemas stored in local variables that are never reassigned.

Examples of incorrect code for this rule

value.trim().startsWith('-');
value.trim().endsWith('-');

Examples of correct code for this rule

value.trimStart().startsWith('-');
value.trimEnd().endsWith('-');
value.trim().startsWith('-', 1);
value.trim().startsWith('foo ');

Options

This rule has no options.

Differences from upstream

Rslint skips Zod string constructors called through a default import, a named string import, TypeScript's import z = require('zod'), or bracket access such as z['string'](). It also skips schemas built with constructor options or stored in a local variable that is never reassigned. In these cases, Unicorn v79.0.0 can suggest replacing trim() with trimStart() or trimEnd(), which would throw when building the schema.

In JavaScript, Rslint also preserves trim() when JSDoc identifies the receiver as a non-string value and it is stored in a let variable that is never reassigned.

With a local type alias named z, Unicorn v79.0.0 offers a trimStart() autofix for the schema below. Rslint skips the call because z.string() still uses the Zod import at runtime.

import { z } from 'zod';

function createSchema() {
  type z = string;
  return z.string().trim().startsWith('-');
}

Original documentation