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/prefer-math-min-max.md.
close

prefer-math-min-max

Unreleased

Configuration

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

export default defineConfig([
  unicornPlugin.configs.recommended,
  {
    rules: {
      'unicorn/prefer-math-min-max': 'error',
    },
  },
]);

Rule details

Prefer Math.min() and Math.max() for ternaries that compare two values and return one of those same values.

The rule skips comparisons involving known BigInt values, new Date() operands, and bindings with non-number TypeScript annotations or initializers such as string literals.

Examples of incorrect code for this rule

height > 50 ? 50 : height;
height < 50 ? 50 : height;

Examples of correct code for this rule

Math.min(height, 50);
Math.max(height, 50);

Fixes and suggestions

The rule offers editor suggestions because the replacement can change the result for NaN, signed zero, or values that need numeric coercion. When a is NaN, a > b ? a : b returns b while Math.max(a, b) returns NaN.

You still get a diagnostic when the replacement would remove a comment or change how often an operand with side effects runs.

Options

This rule has no options.

Differences from upstream

Rslint reports without a suggestion when Math is shadowed or disabled in languageOptions.globals.

function cap(Math, height) {
  return height > 50 ? 50 : height;
}

Rslint also reports without a suggestion for delete expressions and classes with side effects in an extends expression, field initializer, or static block. For example, replacing this ternary could change how many times the property is deleted.

const capped = (delete cache.key, height) > 50 ? 50 : (delete cache.key, height);

When linting a file without a TypeScript project, Rslint also skips comparisons that use the result of a callback typed as () => bigint or a function whose return type is bigint & unknown or a signed BigInt literal type like -1n. Passing these values to Math.min() or Math.max() throws a TypeError.

declare const read: (() => bigint);
const value = read();
value > 0 ? value : 0;

function readLimit(): bigint & unknown {
  return 1n;
}
const limit = readLimit();
limit > 0 ? limit : 0;

Original documentation