In TypeScript, the number type is quite flexible and can represent several kinds of numeric values, but there are no distinct “types” for each kind of number (e.g., integers, floats). TypeScript simply has one number type, which can represent:

  1. Decimal numbers (base 10)
  2. Hexadecimal numbers (base 16, starting with 0x)
  3. Binary numbers (base 2, starting with 0b)
  4. Octal numbers (base 8, starting with 0o)

TypeScript doesn’t differentiate between these kinds of numbers in terms of type, but it allows you to represent them in various forms.

Here’s a summary of the types of numeric literals that can be represented in TypeScript:

Types of Numbers in TypeScript

  1. Decimal Numbers (base 10):

    let decimal: number = 42;
  2. Hexadecimal Numbers (base 16, prefixed with 0x):

    let hex: number = 0x2A;  // Equivalent to decimal 42
  3. Binary Numbers (base 2, prefixed with 0b):

    let binary: number = 0b101010;  // Equivalent to decimal 42
  4. Octal Numbers (base 8, prefixed with 0o):

    let octal: number = 0o52;  // Equivalent to decimal 42
  5. Floating-Point Numbers (representing decimals, not specifically limited to an integer):

    let float: number = 3.14;
  6. Special Number Values:

    • Infinity: A special constant representing infinity.

      let infinite: number = Infinity;
    • NaN (Not-a-Number): Represents an invalid number result.

      let invalidNumber: number = NaN;

Example with All Types

let decimal: number = 42;
let hex: number = 0x2A;
let binary: number = 0b101010;
let octal: number = 0o52;
let float: number = 3.14;
let infinite: number = Infinity;
let invalidNumber: number = NaN;