TypeScript Essentials for Angular
Why TypeScript?
TypeScript is JavaScript with types. The editor warns you about mistakes before you run the code. Angular is written in TypeScript and all Angular code uses it.
Basic Types
typescript
let name: string = 'Ana';
let age: number = 30;
let isActive: boolean = true;
let tags: string[] = ['a', 'b'];
let pair: [string, number] = ['id', 1]; // tuple
let anything: unknown = 5; // safe "any"
const city = 'Delhi'; // type is inferred as string
age = 'thirty'; // ERROR: string is not a number
Union Types and Literals
typescript
type Status = 'loading' | 'success' | 'error';
let status: Status = 'loading';
let id: number | string = 5;
let nickname: string | null = null;
Interfaces and Type Aliases
typescript
interface User {
id: number;
name: string;
email?: string; // optional
readonly createdAt: Date; // cannot change
}
const user: User = { id: 1, name: 'Ana', createdAt: new Date() };
type Point = { x: number; y: number };
Interface or type? For objects, either works. Use
interface for data shapes and type for unions.
Functions
typescript
function add(a: number, b: number): number {
return a + b;
}
const multiply = (a: number, b = 2): number => a * b; // arrow + default value
const log = (msg: string, ...extra: string[]): void => console.log(msg, extra);
Classes
typescript
class Account {
private balance = 0; // only inside this class
constructor(public owner: string) {} // creates the property automatically
deposit(amount: number): void {
if (amount <= 0) throw new Error('Invalid amount');
this.balance += amount;
}
get total(): number {
return this.balance;
}
}
const acc = new Account('Ana');
acc.deposit(100);
console.log(acc.total); // 100
| Modifier | Access |
|---|---|
public (default) | Everywhere |
private | Inside the class |
protected | Class and subclasses |
readonly | Cannot be changed after setting |
Modern JavaScript You Will Use Daily
typescript
const { name, email } = user; // destructuring
const [first, ...rest] = [1, 2, 3]; // array destructuring + rest
const copy = { ...user, name: 'Bob' }; // spread: copy and change
const all = [...list1, ...list2];
const city = user.address?.city; // optional chaining
const label = user.nickname ?? 'No nickname'; // nullish coalescing
const names = users.map(u => u.name);
const adults = users.filter(u => u.age >= 18);
const total = items.reduce((sum, i) => sum + i.price, 0);
Generics
typescript
function first<T>(items: T[]): T | undefined {
return items[0];
}
interface ApiResponse<T> {
data: T;
total: number;
}
const res: ApiResponse<User[]> = { data: [], total: 0 };
Enums
typescript
enum Role { Admin = 'ADMIN', User = 'USER' }
const r: Role = Role.Admin;
async and await, Promises
typescript
async function loadUser(id: number): Promise<User> {
const response = await fetch(`/api/users/${id}`);
if (!response.ok) throw new Error('Not found');
return response.json();
}
Modules: import and export
typescript
// user.model.ts
export interface User { id: number; name: string; }
// another file
import { User } from './user.model';
Decorators
Angular uses decorators such as @Component to attach information to a class. You will not write your own, only use them.
typescript
@Component({ selector: 'app-demo', template: '<p>Demo</p>' })
export class Demo {}
Turn strict mode on: New Angular projects use
strict: true. It feels demanding at first but prevents many runtime errors. Avoid the type any.
Conclusion
Learn types, interfaces, classes, arrow functions, destructuring, spread, optional chaining, generics and async/await. They appear in every Angular file.
Quick Summary: Types + interfaces + classes + modern JS = ready for Angular.

