3️⃣ 🧪 TypeScript Core & Special Types
Estimated reading: 4 minutes 44 views

🧰 TypeScript – Simple Types Explained with Syntax, Examples & Use Cases

🧲 Introduction – Understanding Simple Types in TypeScript

TypeScript is a statically typed superset of JavaScript, which means it adds a powerful type system on top of standard JavaScript. One of the foundational features of TypeScript is its support for simple (or primitive) types—used to describe the most basic building blocks of your code.

🎯 In this guide, you’ll explore:

  • What are simple (primitive) types in TypeScript
  • Syntax and usage of each type
  • Code examples with real-world context
  • Type annotations and type inference
  • Best practices for safer and cleaner code

📚 What Are Simple Types in TypeScript?

Simple types, also known as primitive types, represent the most fundamental data values in a TypeScript program. These types include:

🔠 Type📄 Description
numberAny numeric value (integer or float)
stringTextual data enclosed in quotes
booleanRepresents true or false
nullExplicitly represents “no value”
undefinedA variable that has been declared but not set
bigintFor very large integers
symbolFor unique identifiers

🔢 1. number Type

The number type handles both integers and floating-point values.

📌 Syntax:

let age: number = 30;
let price = 199.99;

✅ Explanation:

  • age is explicitly typed as a number.
  • price is inferred to be a number based on the assigned value.
  • TypeScript uses number for all numeric values (no distinction between int or float).

🔤 2. string Type

The string type is used for textual data enclosed in ' ', " ", or “ (template literals).

📌 Syntax:

let name: string = "Alice";
let greeting = `Hello, ${name}!`;

✅ Explanation:

  • Strings can be declared using double quotes, single quotes, or backticks.
  • Template literals (` `) allow interpolation with ${} syntax.

✅ 3. boolean Type

The boolean type only accepts true or false.

📌 Syntax:

let isLoggedIn: boolean = true;
let hasAccess = false;

✅ Explanation:

  • isLoggedIn is explicitly typed.
  • hasAccess is inferred as a boolean.
  • Useful for flags, toggles, conditions.

❌ 4. null and undefined Types

Both null and undefined represent absence of a value, but they are used differently.

📌 Syntax:

let emptyValue: null = null;
let uninitialized: undefined = undefined;

✅ Explanation:

  • null is often assigned intentionally to variables.
  • undefined occurs when a variable is declared but not assigned.

🔹 Strict Mode Tip: Enable strictNullChecks in tsconfig.json to make your code more type-safe by catching potential null/undefined bugs.


🔢 5. bigint Type

Introduced in ES2020, bigint allows you to store arbitrarily large integers.

📌 Syntax:

let bigValue: bigint = 9007199254740991n;

✅ Explanation:

  • Append n at the end to denote a bigint literal.
  • Use when working with very large numbers (e.g., cryptography, blockchain).

🧩 6. symbol Type

A symbol is a unique, immutable identifier, often used for object property keys.

📌 Syntax:

let id: symbol = Symbol("userID");

✅ Explanation:

  • Symbol() creates a unique value even if the descriptions are the same.
  • Useful for adding non-colliding keys in object properties.

✍️ Type Annotation vs. Type Inference

TypeScript allows both:

✅ Explicit Type Annotation:

let score: number = 100;

✅ Type Inference (Recommended for simple cases):

let username = "john_doe"; // Inferred as string

🔹 Tip: Prefer inference when the value clearly implies the type. Use annotations for clarity in complex logic or function signatures.


⚠️ Common Mistakes & How to Avoid Them

❌ Mistake💡 Solution / Fix
Assigning null to stringUse union type: `string
Using variables without initializationAlways initialize or use type undefined
Forgetting n with bigintUse: let big: bigint = 1000n
Mixing number and bigintAvoid operations between number and bigint

💡 Best Practices

  • ✅ Use const when value won’t change (e.g., const PI = 3.14)
  • ✅ Enable strict and strictNullChecks in your config
  • ✅ Use type inference for simple declarations
  • 🚫 Avoid using any unless absolutely necessary

📌 Summary – Recap & Next Steps

Simple types are the foundation of strong typing in TypeScript. They help developers write clear, bug-resistant, and self-documenting code.

🔍 Key Takeaways:

  • Use number, string, boolean, null, undefined, bigint, and symbol for base data modeling
  • Combine simple types using unions (string | null)
  • Prefer inference over explicit typing unless clarity is needed
  • Enable strict checks for better runtime safety

⚙️ Real-world relevance: Simple types are essential for functions, interfaces, data models, and more—no TypeScript project can function without them.


❓ FAQs – TypeScript Simple Types

❓ Is number the same for integers and floats in TypeScript?
✅ Yes. There is only one numeric type: number. It covers both.

❓ Can I assign null to a string?
❌ Not unless you use a union: let name: string | null.

❓ What’s the difference between undefined and null?
undefined means “value not assigned”, null means “no value intentionally”.

❓ Can I use bigint in all browsers?
❌ Not yet. It’s supported in modern environments—check compatibility or use a polyfill.

❓ When should I use symbol?
✅ Use symbol for object properties you want to keep unique and non-enumerable.


Share Now :

Leave a Reply

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

Share

TS Simple Types / TypeScript – Types

Or Copy Link

CONTENTS
Scroll to Top