Number Type in TypeScript

Number type in TypeScript represents the numeric values similar to JavaScript. All the numbers are represented as floating point values in TypeScript. TypeScript also supports the binary, octal and hexadecimal numbers introduced in ECMAScript 2015.

In ES2020 a new keyword binint was added to represent Big Integers.

Explicitly declaring a variable as number using type annotation.

let max_load : number ; 

Variable declaration and initialization example.

let max_load : number = 10;

Even if we declare a variable without type annotation, TypeScript will infer the type using type inference.

let x = 20;

Before we move forward it is important to understand that in TypeScript number and Number are two different concepts

In TypeScript, we can create a number primitive as a Number object. But there is a catch Type 'Number' is not assignable to type 'number' because ‘number’ is a primitive, but ‘Number’ is a wrapper object. Prefer using ‘number’ when possible. We cannot reassign a Number class instance to a primitive number.

let’s take an example to understand the difference.

// Declare two variables with primitive 'number' and interface 'Number'.
let counterOne : number = 250; // primitive number

let counterTwo : Number = new Number(500); // Instance of Number object

Now let’s try assigning one value to another and see what response we get.

// assigning 

counterTwo = counterOne 
// No error as 'number' can easily be assigned to 'Number' object. 

counterOne = counterTwo 
// Error :  Type 'Number' is not assignable to type 'number'

Key difference between ‘number’ and ‘Number’

  • number is a primitive type, while Number is an object type.
  • Number values are stored as JavaScript numbers (64-bit floating-point numbers), whereas Number objects wrap these numbers and provide additional methods.

Decimal Numbers

Examples of decimal number initialization.

let interest : number = 7.6;

// multiple variable initialization 
let length : number = 9.8 , width : number = 6.5;

Binary Numbers

Binary numbers uses alphanumeric characters where first character is a '0' (ZERO) followed by either letter 'B' or 'b'.

let binary_number : number = 0b100;

// multiple variable initialization 
let binary_one : number = 0B100 , binary_two : number = 0b010;

NOTE : Digits after alphanumeric '0b'or '0B' should be either 0 or 1

Octal Numbers

Octal numbers use combination of letter '0' (ZERO) and 'o' (alphabet o) represented as '0o' at start of number to identify its an octal number, this improvement was part of ES2015 update to TypeScript language.

let octal_number : number = 0o50;

Hexadecimal Numbers

Hexadecimal numbers use combination of letter '0' (ZERO) and 'x' or 'X' (alphabet x) represented as '0x' or '0X' at start of number to identify its an hexadecimal number. The remaining number should be constructed from list of allowed characters which are '0123456789ABCDEF'.

let hexadecimal_number : number = 0XB;

Big Integers

Big Integers represented using key word bigint, which is not available below ES2020. Big integers are used to store whole numbers greater than 253 – 1. Big integers are identified using ‘n’ character at the end of integer.

let big_number : bigint = 165165165165n;

If we try using bigint for target environments below ES2020, we will receive TypeScript error 'BigInt literals are not available when targeting lower than ES2020.'.

Summary

  • numbers in TypeScript can hold integers, decimal point, octal and hexadecimal numbers.
  • numbers and Numbers are two different concepts, one is primitive while second is an instance of Number.
  • Most of the time we use number only to represent numerical data.
  • bigint is not available for target environment below ES2020.
Scroll to Top