Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

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
leftOperand < rightOperand
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
// Valid: Numeric comparison
const isLessNum: boolean = 5 < 10; // true

// Valid: BigInt comparison
const isLessBigInt: boolean = 100n < 200n; // true

// Valid: Lexicographical string comparison
const isLessStr: boolean = "apple" < "banana"; // true

// Valid: Date comparison
const date1 = new Date("2023-01-01");
const date2 = new Date("2023-12-31");
const isLessDate: boolean = date1 < date2; // true

// Valid: Enum comparison
enum Priority { Low, Medium, High }
const isLessEnum: boolean = Priority.Low < Priority.High; // true
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.
const num: number = 5;
const str: string = "10";

// TS2365: Operator '<' cannot be applied to types 'number' and 'string'.
const invalidPrimitiveComparison = num < str; 

const obj1 = { val: 1 };
const obj2 = { val: 2 };

// TS2365: Operator '<' cannot be applied to types '{ val: number; }' and '{ val: number; }'.
const invalidObjectComparison = obj1 < obj2;
To resolve type mismatch errors, explicit type casting or property extraction is required to yield valid, mutually comparable types before evaluation.
// Validated through explicit conversion
const validStringComparison: boolean = num < Number(str);

// Validated through property extraction
const validObjectComparison: boolean = obj1.val < obj2.val;
Master TypeScript with Deep Grasping Methodology!Learn More