DEV Community

Cover image for A Quick Introduction To Types In TypeScript
Valentine Samuel
Valentine Samuel

Posted on

2

A Quick Introduction To Types In TypeScript

Typescript is a superset of JavaScript that provides more robust features and error checking at compile time. One of the primary features of Typescript is its strong typing system, which provides support for various types. Understanding the types available in Typescript is essential for any developer to write clean, reliable, and bug-free code. In this article, we will explore all the types in Typescript and categorize them into basic types and advanced types.

Basic Types:

  • Number: The Number type represents both integer and floating-point numbers. It supports all standard mathematical operations, such as addition, subtraction, multiplication, and division. Here is an example.
let num: number = 5;
console.log(num); // Output: 5
Enter fullscreen mode Exit fullscreen mode
  • String: The String type represents a sequence of characters. It is enclosed in quotes, either single or double. Here is an example:
let str: string = 'Hello, World!';
console.log(str); // Output: Hello, World!
Enter fullscreen mode Exit fullscreen mode
  • Boolean: The Boolean type represents a logical value, either true or false. Here is an example:
let isTrue: boolean = true;
console.log(isTrue); // Output: true
Enter fullscreen mode Exit fullscreen mode
  • Null: The Null type represents an intentional absence of any object value. Here is an example:
let nullValue: null = null;
console.log(nullValue); // Output: null
Enter fullscreen mode Exit fullscreen mode
  • Undefined: The Undefined type represents a variable that has been declared but has not been assigned a value. Here is an example:
let undefinedValue: undefined = undefined;
console.log(undefinedValue); // Output: undefined
Enter fullscreen mode Exit fullscreen mode

Advanced Types:

These are types that are made up of one or more basic types.

  • Array: The Array type represents a collection of elements of the same type or different types, it is recommended to make arrays hold only a collection of one particular type. It can be declared using square brackets. Here is an example:
let arr: number[] = [1, 2, 3, 4, 5];
console.log(arr); // Output: [1, 2, 3, 4, 5]
Enter fullscreen mode Exit fullscreen mode
  • Tuple: The Tuple type represents an array with a fixed number of elements, each with its type. For tuples, the order of appearance must match. A number cannot come in the position of a string. Here is an example:
let tuple1: [string, number] = ['John', 25];
// let tuple1: [string, number] = [25, 'John'];❌
console.log(tuple1); // Output: ['John', 25]
Enter fullscreen mode Exit fullscreen mode
  • Enum: The Enum type is a way of giving more friendly names to sets of numeric values. Here is an example:
enum Color {Red, Green, Blue};
let c: Color = Color.Green;
console.log(c); // Output: 1
Enter fullscreen mode Exit fullscreen mode
  • Any: The Any type represents any value, and it can be assigned to any variable without a type check. Any type is equivalent to writing plain javascript and its usage is very much discouraged. Here is an example:
let anyValue: any = 'Hello, World!';
console.log(anyValue); // Output: Hello, World!
Enter fullscreen mode Exit fullscreen mode
  • Void: The Void type represents the absence of any type at all. It is commonly used as the return type of function that does not return a value. Here is an example:
function logMessage(): void {
  console.log('Hello, World!');
}
logMessage(); // Output: Hello, World!
Enter fullscreen mode Exit fullscreen mode



In conclusion, Typescript provides a rich set of types that enable developers to write more reliable code. Basic types are the fundamental types that are commonly used in everyday programming, while advanced types are more specialized and provide more functionality. As a beginner, understanding these types is essential to mastering Typescript and writing better code.

Don't forget to leave a like or comment. Till next time guys.

❤️❤️❤️

Resume Portfolio Twitter GitHub LinkedIn

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

Cloudinary image

Optimize, customize, deliver, manage and analyze your images.

Remove background in all your web images at the same time, use outpainting to expand images with matching content, remove objects via open-set object detection and fill, recolor, crop, resize... Discover these and hundreds more ways to manage your web images and videos on a scale.

Learn more

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay