Topics in this subject
TypeScript 3 min read Updated 11 Aug 2026

Basic Types and Primitives

Learn about string, number, boolean, array, tuple, any, unknown, never, and void.

🧑‍🏫 Sabse pehle — simple mein samjho#

Jaise duniya mein har cheez ki ek category hoti hai (jaise Paani = Liquid, Pathar = Solid), waise hi programming mein data ki categories hoti hain. TypeScript mein hum inhi categories ko clearly batate hain. Basic types (primitives) sabse choti building blocks hote hain — inke aage koi complexity nahi hoti.

The Big Three (String, Number, Boolean)#

These three are the most commonly used primitive types.

// 1. String: For text
let username: string = "Tarun";

// 2. Number: For integers and floats (JS treats all numbers identically)
let age: number = 25;
let price: number = 99.99;

// 3. Boolean: For True or False values
let isLoggedIn: boolean = true;

Arrays#

There are two ways to define the type of an Array. Both achieve the exact same result.

// Syntax 1 (Commonly used)
let scores: number[] = [10, 20, 30];
let fruits: string[] = ["Apple", "Mango"];

// Syntax 2 (Generics syntax)
let ages: Array<number> = [25, 30, 35];

Tuples (Fixed length & type Array)#

Sometimes you need an array with a fixed number of elements, where each element has its own specific type. (React's useState hook returns a tuple!)

// An array where the first element must be a string, and the second a number
let userRecord: [string, number] = ["Tarun", 1];

userRecord[0] = "Rana"; // ✅ OK
userRecord[1] = "Two"; // ❌ ERROR: Type 'string' is not assignable to type 'number'

Special Types: any, unknown, void, never#

any (The Escape Hatch)#

When you use any, TypeScript completely turns off type checking for that variable. It feels like writing plain JavaScript, but it is highly dangerous. Avoid this!

let myValue: any = "Hello";
myValue = 10; // ✅ OK
myValue.fakeMethod(); // ✅ TS throws no error, but the app will crash at runtime

unknown (The Safe version of any)#

unknown means "I don't know what this type is right now". However, unlike any, TS will not let you perform any operations on it until you confirm its actual type. It is much better than any for handling API responses.

let secret: unknown = "Hidden String";

// secret.toUpperCase(); // ❌ ERROR: Object is of type 'unknown'

if (typeof secret === "string") {
    secret.toUpperCase(); // ✅ Works now because TS knows it's a string
}

void (Nothing returned)#

When a function does not return anything, its return type should be void.

function logMessage(msg: string): void {
    console.log(msg);
    // No return statement needed here
}

never (Impossible state)#

Some functions never finish executing (e.g., they always throw an error or contain an infinite loop). Their return type is never.

function crashApp(message: string): never {
    throw new Error(message);
    // The code will never reach beyond this point
}