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 > (greater than) operator is a relational binary operator that evaluates whether its left operand is strictly greater than its right operand, returning a boolean value (true or false).
leftOperand > rightOperand
In TypeScript, the > operator applies strict compile-time type checking to restrict the unpredictable implicit type coercion (Abstract Relational Comparison) that occurs in standard JavaScript.

Type Compatibility Rules

TypeScript requires operands to be of compatible, comparable types. The compiler permits the > operator for the following types:
  1. Numeric Types (number, bigint): Evaluates the algebraic magnitude. TypeScript explicitly allows cross-type relational comparisons between number and bigint. Comparisons involving NaN always evaluate to false.
  2. String Types (string): Evaluates lexicographically based on the sequence of 16-bit unsigned integer values (UTF-16 code units).
  3. Enums (Numeric and String): Numeric enums are compared by their underlying numeric values. String enums are assignable to the string type and are compared lexicographically.
  4. Date Objects (Date): Evaluates based on their underlying numeric time value (milliseconds since the epoch) via the internal valueOf() method.
// Valid syntax and type comparisons
const numCheck: boolean = 10 > 5;           // true
const bigIntCheck: boolean = 10n > 5n;      // true
const mixedNumCheck: boolean = 10 > 5n;     // true (number > bigint is allowed)
const strCheck: boolean = "Z" > "A";        // true

enum StringEnum { A = "A", B = "B" }
const strEnumCheck: boolean = StringEnum.B > StringEnum.A; // true

const dateCheck: boolean = new Date("2024-02-01") > new Date("2024-01-01"); // true

Compiler Errors

If you attempt to use the > operator on incompatible types, TypeScript will throw a compile-time error (TS2365). While number and bigint can be mixed, TypeScript intentionally blocks all other cross-type comparisons (e.g., number > string). It also strictly blocks comparisons between booleans, arrays, and complex objects (excluding Date), unless explicitly bypassed using the any type.
// TS2365: Operator '>' cannot be applied to types 'number' and 'string'.
const invalidMixed = 10 > "5"; 

// TS2365: Operator '>' cannot be applied to types 'boolean' and 'boolean'.
const invalidBool = true > false; 

// TS2365: Operator '>' cannot be applied to types 'number[]' and 'number[]'.
const invalidArray = [1, 2] > [1]; 

Bypassing Type Checks

If operands are typed as any, TypeScript defers to the JavaScript runtime behavior. The JavaScript engine will attempt to coerce the operands into primitives (preferring numbers) using the internal ToPrimitive and ToNumeric abstract operations before performing the comparison.
const left: any = [2];
const right: any = 1;

// Compiles successfully due to 'any'. 
// At runtime, [2] is coerced to "2", then to 2. 2 > 1 evaluates to true.
const bypassedCheck: boolean = left > right; 
Master TypeScript with Deep Grasping Methodology!Learn More