Skip to main content
The < (less than) operator is a relational binary operator that evaluates whether its left operand is strictly lesser in value than its right operand, yielding a boolean result. TypeScript enhances this standard JavaScript operator by applying static type checking to the operands, preventing unsafe cross-type comparisons that rely on implicit type coercion. Syntax
Type Evaluation Rules TypeScript restricts the < operator to specific types. Both operands must be of a mutually comparable type. Valid types include number, string, bigint, Date, enum, and any.
  • Numeric Evaluation: When operands are number, bigint, or numeric enum types, the operator performs a standard mathematical comparison.
  • Lexicographical Evaluation: When operands are string types, the operator compares the sequence of 16-bit unsigned integer values (UTF-16 code units) from left to right.
  • Date Evaluation: When both operands are Date instances, TypeScript permits the comparison natively. At runtime, JavaScript implicitly calls the valueOf() method on the objects, comparing their numeric epoch timestamps.
  • Any Evaluation: If one or both operands are of type any, the compiler bypasses strict type checking, deferring to JavaScript’s runtime abstract relational comparison algorithm.
Code Visualization
Compiler Restrictions If you attempt to compare incompatible types, or attempt to use the < operator on unsupported complex types like plain objects or arrays, the TypeScript compiler rejects the operation and emits error TS2365. This is a strict compiler rule designed to prevent the unpredictable type coercion inherent to the underlying JavaScript engine.
To resolve type mismatch errors, explicit type casting or property extraction is required to yield valid, mutually comparable types before evaluation.
Tired of Poor TypeScript Skills? Fix That With Deep Grasping!Learn More