Sling Academy
Home/TypeScript/Working with Numbers in TypeScript

Working with Numbers in TypeScript

Last updated: January 07, 2024

Introduction

TypeScript adds additional features to JavaScript such as static typing. Working with numbers in TypeScript is straightforward because it builds on the existing JavaScript capabilities, yet offers benefits like type safety and better tooling to catch errors early in the development process.

Basic Number Operations

In TypeScript, numbers are used in the same way as in JavaScript. You can do basic arithmetic operations such as addition, subtraction, multiplication, and division using number literals:

let sum: number = 10 + 5;
let difference: number = 10 - 5;
let product: number = 10 * 5;
let quotient: number = 10 / 5;

Always specify the type number when declaring a variable to hold numeric values for better type-checking.

Type Assertions

TypeScript allows you to override its inferred and analyzed views of types in any way you want to. This is done using type assertions:

let someValue: any = '123';
let strLength: number = (<number>someValue).length;
// Or using the as-syntax
let strLengthAs: number = (someValue as string).length;

Special Numeric Types

In addition to the general number type, TypeScript defines two special numeric types: NaN for ‘Not-a-Number’ and Infinity.

let notANumber: number = NaN;
let infinityNumber: number = Infinity;

Enums and Numeric Types

Enums in TypeScript can also be numeric. Enums are a way of giving more friendly names to sets of numeric values:

enum Status { Ready, Waiting };
let statusReady: Status = Status.Ready;

Advanced Operations: Numeric Separators and BigInt

TypeScript 3.7 introduced the capability to include numeric separators in numeric literals to improve readability. Additionally, BigInt is a built-in object that provides a way to represent whole numbers larger than 253 – 1, which is the largest number JavaScript can reliably represent with the Number primitive.

let largeNumber: bigint = 1234567890123456789012345678901234567890n;

// With separator
let readableNumber: number = 1_000_000_000_000;

Working with Number Objects

TypeScript, similar to JavaScript, can work with Number objects. A Number object is created by using the Number() constructor.

let numObj: Number = new Number(10);

Type Inference

TypeScript will infer types during variable initialization. If a number is assigned to a variable, TypeScript will treat that variable as a number type moving forward, reducing the need to explicitly define the type annotation:

let inferredNumber = 10; // TypeScript infers the number type

Conclusion

To effectively work with numbers in TypeScript, leverage the language’s type system to avoid common mistakes, use enums to make code more readable, and BigInt for handling very large numbers. Keeping the code well-typed will lead to more maintainable and reliable software.

Next Article: Working with Boolean in TypeScript

Previous Article: TypeScript: Complete ‘Hello World’ Example

Series: The First Steps to TypeScript

TypeScript

You May Also Like

  • TypeScript: setInterval() and clearInterval() methods (3 examples)
  • TypeScript sessionStorage: CRUD example
  • Using setTimeout() method with TypeScript (practical examples)
  • Working with window.navigator object in TypeScript
  • TypeScript: Scrolling to a specific location
  • How to resize the current window in TypeScript
  • TypeScript: Checking if an element is a descendant of another element
  • TypeScript: Get the first/last child node of an element
  • TypeScript window.getComputerStyle() method (with examples)
  • Using element.classList.toggle() method in TypeScript (with examples)
  • TypeScript element.classList.remove() method (with examples)
  • TypeScript: Adding Multiple Classes to An Element
  • element.insertAdjacentHTML() method in TypeScript
  • TypeScript – element.innerHTML and element.textContent
  • Using element.removeAttribute() method in TypeScript
  • Working with Document.createElement() in TypeScript
  • Using getElementById() method in TypeScript
  • Using Window prompt() method with TypeScript
  • TypeScript – window.performance.measure() method (with examples)