"Testing1"
TypeScript Basic Types
TypeScript: Basic Types
- TypeScript helps you clearly define what kind of data you're working with.
- In regular JavaScript, variables can be anything — a number, a string, an object... and that can lead to confusing bugs.
- With TypeScript, you can declare the kind of value a variable should hold. This makes your code more reliable.
Common Types in TypeScript
Here’s a quick guide to some basic data types:
1. number
Used for all numeric values — whole numbers, decimals, etc.
let age: number = 25;
2. string
Used for text — anything inside quotes.
let name: string = "Sahand";
3. boolean
Represents true or false values.
let isActive: boolean = true;
4. array
Used to store a list of values of the same type.
let scores: number[] = [90, 85, 100];
5. any
Can be any type — useful when you’re not sure what kind of value you’ll get.
let something: any = "Hello"; Something = 42; // also valid
Avoid using any too much — it defeats the purpose of type checking!
6. object
Used for structured data with key-value pairs.
let user: { name: string; age: number } = {
name: "Sahand",
age: 30,
};
7. tuple
An array with a set number of items, where each spot can hold a different kind of value.
let userInfo: [string, number] = ["Sahand", 30];
8. enum
A way to give names to sets of values.
enum Direction {
Up,
Down,
Left,
Right,
}
Let move: Direction = Direction.Left;
9. undefined & null
Used to represent empty or unknown values.
let data: undefined = undefined; Let nothing: null = null;
Why Use Types?
Using these types lets TypeScript help you write clean, bug-free code by warning you when something doesn’t match the expected data format.
Prefer Learning by Watching?
Watch these YouTube tutorials to understand TYPESCRIPT Tutorial visually:
What You'll Learn:
- 📌 TypeScript tutorial for beginners #5 Types | number | string etc
- 📌 TypeScript #3 - Basic Types