🧰 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 |
|---|---|
number | Any numeric value (integer or float) |
string | Textual data enclosed in quotes |
boolean | Represents true or false |
null | Explicitly represents “no value” |
undefined | A variable that has been declared but not set |
bigint | For very large integers |
symbol | For 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:
ageis explicitly typed as a number.priceis inferred to be a number based on the assigned value.- TypeScript uses
numberfor all numeric values (no distinction betweenintorfloat).
🔤 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:
isLoggedInis explicitly typed.hasAccessis 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:
nullis often assigned intentionally to variables.undefinedoccurs 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
nat 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 string | Use union type: `string |
| Using variables without initialization | Always initialize or use type undefined |
Forgetting n with bigint | Use: let big: bigint = 1000n |
Mixing number and bigint | Avoid operations between number and bigint |
💡 Best Practices
- ✅ Use
constwhen value won’t change (e.g.,const PI = 3.14) - ✅ Enable
strictandstrictNullChecksin your config - ✅ Use type inference for simple declarations
- 🚫 Avoid using
anyunless 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, andsymbolfor 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 :
