TypeScript Cheatsheet
Comprehensive TypeScript reference: types, interfaces, generics, classes, and decorators.
| Keyword | Description | Syntax | Example | Category |
|---|---|---|---|---|
| string | Text data type | const name: string = "value" | const name: string = "John" | Types |
| number | Numeric data type | const age: number = 25 | const score: number = 95.5 | Types |
| boolean | True/false data type | const active: boolean = true | const isAdmin: boolean = false | Types |
| any | Disable type checking (avoid) | const x: any = value | const x: any = "anything" | Types |
| unknown | Safe alternative to any | const x: unknown = value | const x: unknown = JSON.parse(data) | Types |
| never | Type that never occurs | function error(): never { throw new Error() } | const x: never = neverReturns() | Types |
| union | Multiple possible types | const x: string | number | const id: string | number = 123 | Types |
| literal | Specific literal value | const x: "yes" | "no" | const status: "active" | "inactive" = "active" | Types |
| Array | Array type declaration | const arr: string[] or Array<string> | const nums: number[] = [1, 2, 3] | Types |
| Tuple | Fixed-length array with types | const tuple: [string, number] = ["a", 1] | const coords: [number, number] = [10, 20] | Types |
| interface | Define object contract | interface Name { property: type } | interface User { id: number; name: string } | Interfaces |
| type | Create type alias | type Name = { property: type } | type User = { id: number; name: string } | Interfaces |
| extends | Extend interface/class | interface Child extends Parent { ... } | interface Admin extends User { role: string } | Interfaces |
| Generic <T> | Parameterized types | function func<T>(arg: T): T { ... } | function identity<T>(x: T): T { return x } | Generics |
| Generic Constraint | Limit generic types | function <T extends Type> | function getLength<T extends { length: number }>(obj: T) { return obj.length } | Generics |
| Keyof | Get keys of a type | type Keys = keyof Type | type UserKeys = keyof User | Advanced |
| Typeof | Get type of a value | type Type = typeof value | type StringType = typeof "hello" | Advanced |
| Conditional Types | Types based on conditions | T extends U ? X : Y | type Check<T> = T extends string ? "yes" : "no" | Advanced |
| Mapped Types | Create types from existing types | type Readonly<T> = { readonly [K in keyof T]: T[K] } | type ReadonlyUser = { readonly [K in keyof User]: User[K] } | Advanced |
| class | Define a class | class Name { property: type; constructor() {} } | class User { name: string; constructor(name: string) { this.name = name } } | Classes |
| private | Private class member | private property: type | private password: string | Classes |
| protected | Protected class member | protected property: type | protected id: number | Classes |
| readonly | Read-only property | readonly property: type | readonly createdAt: Date | Classes |
| @decorator | Class decorator | @decorator class Name { ... } | @sealed class User { ... } | Decorators |