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 or equal to) operator is a binary relational operator that evaluates to the boolean true if the left operand’s value is mathematically, lexicographically, or chronologically less than or identical to the right operand’s value, and false otherwise.
leftOperand <= rightOperand

TypeScript Type Constraints

TypeScript applies strict static type checking to the <= operator to prevent the unpredictable implicit type coercion inherent in standard JavaScript’s Abstract Relational Comparison.
  1. Permitted Types: The operands must resolve to type number, bigint, string, Date, an enum type, or any.
  2. Type Homogeneity: TypeScript generally requires both operands to be of the same type. Exceptions include comparisons between number and bigint, and comparisons involving numeric enum types and number.
  3. Compile-Time Errors: Attempting to apply the operator to unsupported types (such as boolean, uncast objects, or arrays) or mixing incompatible types results in a TS2365 compiler error.
enum Size { Small, Medium, Large }

// Valid comparisons
const numCheck: boolean = 5 <= 10;             // true
const strCheck: boolean = "apple" <= "banana"; // true
const bigIntCheck: boolean = 50n <= 50;        // true (mixed number/bigint is valid)
const enumCheck: boolean = Size.Small <= Size.Large; // true
const dateCheck: boolean = new Date("2023-01-01") <= new Date("2024-01-01"); // true

// Invalid comparisons (Compile-time errors)
const err1 = 5 <= "5";       // TS2365: Operator '<=' cannot be applied to types 'number' and 'string'.
const err2 = {} <= {};       // TS2365: Operator '<=' cannot be applied to types '{}' and '{}'.
const err3 = false <= true;  // TS2365: Operator '<=' cannot be applied to types 'boolean' and 'boolean'.

Evaluation Mechanics

When the TypeScript code is compiled and executed, the operator follows specific evaluation algorithms based on the resolved primitive types:
  • Numeric Evaluation (number, bigint, numeric enum): Evaluates mathematical magnitude. For number types, it adheres to IEEE 754 floating-point comparison rules. If either operand evaluates to NaN (Not-a-Number), the operator strictly returns false.
  • String Evaluation (string, string enum): Performs a lexicographical comparison. It compares the strings character by character from left to right, evaluating the 16-bit UTF-16 code unit values of each character.
  • Date Evaluation (Date): Evaluates chronological order. The JavaScript runtime implicitly invokes the valueOf() method on the Date objects, converting them to their primitive numeric timestamp values (milliseconds since the Unix Epoch) before performing a numeric comparison.
  • Bypassed Typing (any): If TypeScript’s static typing is bypassed (e.g., using any or @ts-ignore), the runtime engine falls back to JavaScript’s Abstract Relational Comparison algorithm. This forces implicit type coercion, invoking [Symbol.toPrimitive], valueOf(), or toString() on objects to resolve them to primitives before executing either a numeric or string comparison.
// Enum evaluation mechanics
enum Status { Pending = 1, Active = 2 }
Status.Pending <= 2; // true (enum and number mix is valid)

// String evaluation mechanics (UTF-16 code units)
"Z" <= "a"; // true (Code unit 90 <= 97)

// NaN evaluation mechanics
NaN <= 5;   // false
NaN <= NaN; // false

// Date evaluation mechanics
new Date("2024-01-01") <= new Date("2024-01-01"); // true (compares numeric timestamps)

// Bypassed typing mechanics (Abstract Relational Comparison)
const left: any = [2];
const right: any = "3";
left <= right; // true (Array coerces to "2", then string "2" <= "3")
Master TypeScript with Deep Grasping Methodology!Learn More