What is TypeScript?
TypeScript is JavaScript with type safety. It helps you catch errors before your code runs by checking that you’re using the right types of data everywhere.Type Annotations
TypeScript lets you specify what type of data a variable should hold:const vs let: Variable Declaration
const (Constant)
- Once assigned a value, it cannot be reassigned
- Use for values that never change (URLs, configuration)
- Note: If it’s an object or array, its properties CAN still change
let (Variable)
- The value can change after being assigned
- Use for counters, state, temporary values
When to Use Each?
Default toconst. Only use let when you NEED to change the value.
From our project:
main.ts
Common Types
| Type | Description | Examples |
|---|---|---|
string | Text | "hello", "world" |
number | Numbers | 42, 3.14 |
boolean | True/False | true, false |
string[] | Array of strings | ["a", "b", "c"] |
number[] | Array of numbers | [1, 2, 3] |
null | Explicitly nothing | null |
undefined | Not yet assigned | undefined |
Union Types
Sometimes a value can be one type OR another:main.ts
error field can be:
- A
string(when there’s an error message) null(when there’s no error)
Why Use TypeScript?
1. Catch Errors Early
2. Autocompletion
Your editor knows what properties an object has:3. Self-Documenting Code
Next Steps
Now that you understand the basics, let’s explore:- Interfaces - Define object shapes
- Enums - Named constants
- Async/Await - Handle asynchronous operations