Skip to content

unnecessaryMathClamps

Reports unnecessary Math.min and Math.max calls with constant arguments or incorrect clamping patterns.

✅ This rule is included in the ts logical presets.

Math.min and Math.max are commonly used to constrain values to specific ranges. However, when all arguments are constant values, the result is always predictable and should be replaced with the constant directly. Additionally, nested Math.min/max calls are often accidentally swapped to never return a value in-range

This rule reports on Math.min and Math.max calls that don’t correctly clamp.

const
const minimum: number
minimum
=
var Math: Math

An intrinsic object that provides basic mathematics functionality and constants.

Math
.
Math.min(...values: number[]): number

Returns the smaller of a set of supplied numeric expressions.

@paramvalues Numeric expressions to be evaluated.

min
(5, 10);
const
const maximum: number
maximum
=
var Math: Math

An intrinsic object that provides basic mathematics functionality and constants.

Math
.
Math.max(...values: number[]): number

Returns the larger of a set of supplied numeric expressions.

@paramvalues Numeric expressions to be evaluated.

max
(3, 7, 2);
const
const clamped: number
clamped
=
var Math: Math

An intrinsic object that provides basic mathematics functionality and constants.

Math
.
Math.max(...values: number[]): number

Returns the larger of a set of supplied numeric expressions.

@paramvalues Numeric expressions to be evaluated.

max
(0,
var Math: Math

An intrinsic object that provides basic mathematics functionality and constants.

Math
.
Math.min(...values: number[]): number

Returns the smaller of a set of supplied numeric expressions.

@paramvalues Numeric expressions to be evaluated.

min
(100,
const value: number
value
));

This rule is not configurable.

If you have a codebase that intentionally uses Math.min/max with constants for code generation or readability purposes, you might choose to disable this rule. However, in most cases, replacing constant Math operations with their computed results makes the code more efficient and clearer.

Made with ❤️‍🔥 around the world by the Flint team and contributors.