prefer-math-min-max
UnreleasedConfiguration
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
Examples of correct code for this rule
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.
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.
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.