Less mixin guards and variable substitution type mismatch behavior
0 reputation · 29 Feb 2020, 00:36 UTC
0 reputation · 29 Feb 2020, 00:36 UTC
Less mixins support reusable rule sets and guard expressions that conditionally apply styles based on variable values. When mixin parameters or variables are substituted inside a guard, the compiler evaluates the expression to determine whether the mixin body should be emitted. A documented but ambiguous area involves type mismatches: if a variable expected to be numeric is passed as a string or keyword, the guard may silently evaluate to false rather than raising a syntax or type error.
This behavior creates uncertainty for teams building shared mixin libraries across projects with differing Less versions. While guard expressions are available in Less 3.0 and later, the exact coercion rules and failure modes for mismatched types inside when conditions are not consistently documented. Developers need to know whether the compiler treats invalid comparisons as a compile-time error, a runtime warning, or a silent skip, and whether that behavior is stable across minor versions.
What is the authoritative evaluation order for variable substitution and type checking inside mixin guards? Does Less emit any diagnostic when a guard comparison involves incompatible types, or is the result always a silent false? Are there configuration flags or strict-mode options that convert these silent mismatches into explicit errors?
35275 reputation · 29 Feb 2020, 09:47 UTC
when guard expression with the substituted values. Type checking is not a separate pass; it is part of expression evaluation after substitution.Guards are conditional by design. If the guard evaluates to false, the mixin is skipped and the compiled CSS simply lacks those rules. Less provides built-in type-checking functions such as isnumber and isstring that can be used inside guards for explicit validation. Guard evaluation occurs after substitution, not before.
Implicit coercion rules inside guards are not formally specified and may vary across Less releases. Because guards are meant to be boolean filters, Less treats a non-matching or non-comparable comparison as false rather than an error. This makes mismatched types fail silently.
isnumber/isstring to avoid silent drops.Which Less major.minor version is used in your build? Coercion edge cases can shift between releases, and confirming the exact version changes the recommendation for safe validation patterns.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.