Chapter 3: TypeScript Simple Types

📘 TypeScript Simple Types (Explained Like a Friendly Teacher)

When we say “Simple Types” in TypeScript, we mean the basic data types that store simple values like numbers, text, true/false, etc.

Remember:

👉 TypeScript is built on top of JavaScript
👉 It adds strong typing to make code safer.

In TypeScript, we clearly tell the computer:

“This variable will store this type of data only.”

That’s the power of types 💪

🎯 Why Simple Types Are Important?

In JavaScript:

In TypeScript:

TypeScript protects your code before running it.

📌 List of Simple Types in TypeScript

  1. string

  2. number

  3. boolean

  4. null

  5. undefined

  6. any

  7. unknown

  8. void

  9. never

Now let’s understand each one clearly with examples.

1️⃣ string Type

Used to store text.

❌ Wrong example:

👉 String must always contain text (inside quotes).

2️⃣ number Type

Stores numbers (integer or decimal).

TypeScript does not separate:

  • int

  • float

  • double

Everything is number.

❌ Wrong:

3️⃣ boolean Type

Stores only:

Real Example:

4️⃣ null Type

Represents intentional empty value.

Mostly used when value is intentionally empty.

5️⃣ undefined Type

Means value is not assigned.

Difference:

6️⃣ any Type (⚠ Use Carefully)

any means:

👉 “I don’t care about type.”

No error ❌

But this removes TypeScript safety.

Use only when necessary.

7️⃣ unknown Type (Safer than any)

unknown is safer version of any.

You cannot directly use it:

You must check type first:

👉 This makes code safer.

8️⃣ void Type

Used mainly in functions that return nothing.

It means:

👉 This function does not return any value.

9️⃣ never Type

Used when something never happens.

Example 1: Function that throws error

Example 2: Infinite loop

never means:
👉 Function never finishes normally.

📊 Quick Comparison Table

Type Stores Example
string Text "Hello"
number Numbers 10, 99.5
boolean True/False true
null Empty value null
undefined Not assigned undefined
any Any type 10, "Hi"
unknown Safe any Needs checking
void No return Functions
never Never ends Errors

🧑‍🏫 Real Life Example (Mini Program)

Output:

Clean and safe code 👍

🔥 Important Concept: Type Inference

TypeScript is smart.

If you write:

TypeScript automatically understands:

This is called Type Inference.

But in large projects, we usually write types clearly.

🏁 Final Definition (Exam Ready)

Simple Types in TypeScript are the basic built-in data types such as string, number, boolean, null, undefined, any, unknown, void, and never that define the kind of value a variable can store, helping developers write safe and error-free code.

If you want next chapter, I can explain:

  • ✅ TypeScript Arrays

  • ✅ TypeScript Objects

  • ✅ TypeScript Functions

  • ✅ TypeScript Union Types

  • ✅ TypeScript Type Aliases

  • ✅ TypeScript Interfaces

Just tell me 😊

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *