TypeScript Cheat Sheet
# TypeScript Cheat Sheet
## Types
```typescript
let name: string = "Alice";
let age: number = 30;
let active: boolean = true;
let items: string[] = ["a", "b"];
let tuple: [string, number] = ["a", 1];
```
## Interfaces & Types
```typescript
interface User {
id: number;
name: string;
email?: string; // optional
readonly id: number; // readonly
}
type Status = "active" | "inactive" | "banned";
type ID = string | number;
```
## Generics
```typescript
function identity<T>(arg: T): T { return arg; }
interface Box<T> { value: T; }
const box: Box<string> = { value: "hello" };
```
## Utility Types
```typescript
Partial<User> // all optional
Required<User> // all required
Pick<User, "name"> // only name
Omit<User, "email"> // without email
Record<string, User> // map of users
ReturnType<typeof fn> // function return type
```
## Enums
```typescript
enum Color { Red = "RED", Green = "GREEN", Blue = "BLUE" }
const c = Color.Red;
```
## Async
```typescript
async function fetchData(): Promise<Data> {
const res = await fetch(url);
return res.json();
}
```
## Mapped & Conditional Types
```typescript
type Nullable<T> = T | null;
type IsString<T> = T extends string ? true : false;
type Keys = keyof User;
```