TypeScript Cheatsheet β Complete Quick Reference (2026)
In this tutorial, you'll learn about TypeScript Cheatsheet. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.
TypeScript adds static typing, interfaces, generics, and advanced type utilities to JavaScript β catching errors at compile time while keeping the same runtime behavior.
Primitive & Compound Types
| Type | Example | Notes |
|---|---|---|
string |
let s: string = "hello" |
|
number |
let n: number = 42 |
All numbers are float |
boolean |
let b: boolean = true |
|
null / undefined |
let x: null = null |
|
void |
function f(): void {} |
No return value |
never |
function e(): never { throw Error() } |
Never returns |
any |
let x: any = 1 |
Opt-out of checking |
unknown |
let x: unknown = 1 |
Safe any β must narrow |
T[] / Array<T> |
let nums: number[] = [1,2] |
Array type |
[string, number] |
let t: [string, number] |
Tuple |
enum |
enum Color { Red, Green, Blue } |
Named constants |
Interfaces & Types
interface User { name: string; age?: number; readonly id: number }
type Point = { x: number; y: number }
type ID = string | number
type Callback = (n: number) => void
Utility Types
| Utility | Example | Effect |
|---------|---------|--------|
| Partial<T> | Partial<User> | All properties optional |
| Required<T> | Required<User> | All properties required |
| Pick<T, K> | Pick<User, 'id' | 'name'> | Select keys |
| Omit<T, K> | Omit<User, 'age'> | Remove keys |
| Record<K, V> | Record<'a' | 'b', number> | Keyβvalue map |
| Exclude<T, U> | Exclude<string | number, string> | Remove from union |
| ReturnType<T> | ReturnType<typeof fn> | Infer return type |
| Parameters<T> | Parameters<typeof fn> | Infer param types |
Generics
function identity<T>(arg: T): T { return arg }
interface Box<T> { value: T }
function first<T>(arr: T[]): T | undefined { return arr[0] }
// constraints
function logLength<T extends { length: number }>(arg: T): number { return arg.length }
Enums
enum Direction { Up = "UP", Down = "DOWN" }
enum Status { Active = 1, Inactive = 0 } // numeric
const enum Size { Small = 1, Medium = 2 } // no runtime cost
Decorators (experimental)
function Log(target: any, key: string) { console.log(`Called ${key}`) }
function Seal(constructor: Function) { Object.seal(constructor.prototype) }
@Seal
class MyClass { @Log method() {} }
tsconfig.json Quick Reference
{
"compilerOptions": {
"target": "ES2022", // output JS version
"module": "NodeNext", // module system
"strict": true, // enable all strict checks
"outDir": "./dist", // output folder
"rootDir": "./src", // source folder
"esModuleInterop": true, // CJS β ESM interop
"skipLibCheck": true // skip .d.ts checking
},
"include": ["src"]
}
Must-Know Items
- Use
strict: truein tsconfig β enablesnoImplicitAny,strictNullChecks,strictFunctionTypes - Prefer
interfacefor public API shapes,typefor unions/intersections - Use
as constfor literal types:const x = "hello" as constβ type"hello" satisfiesoperator validates type without widening:const x = { a: 1 } satisfies Record<string, number>- Use
unknowninstead ofanyto force type narrowing before use - Branded types add nominal typing:
type Email = string & { __brand: 'Email' } keyof Textracts keys of T as a union;T[K]accesses property type- Template literal types:
type EventName = `on${Capitalize<string>}`
{{< faq "What is the difference between interface and type in TypeScript?">}}Interfaces can be extended (merged) and are preferred for object shapes. Type aliases can represent unions, intersections, and primitives, but cannot be reopened to add new properties. Use interfaces for public APIs and type aliases for complex type compositions.{{< /faq >}}
See full TypeScript tutorials for advanced patterns.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro