A TypeScript cheat sheet maps the type layer on top of JavaScript: annotations, interfaces, unions, generics, and narrowing. The compiler erases types before the browser runs the file. Strict tsconfig options (strict, noUncheckedIndexedAccess) catch the bugs this sheet exists to prevent.
This TypeScript cheat sheet is written for people searching for a fast, accurate reference: students, junior developers, and teams shipping production software. Pin it, copy the snippets, and come back when syntax slips.
Quick reference
- Annotate:
const n: number = 1— often inferred; annotate public function boundaries. - Interface:
interface User { id: number; email: string }for object shapes. - Type alias:
type Id = string | numberfor unions, tuples, mapped types. - Union / narrow:
if (typeof x === "string")orin/ discriminatedkindfields. - Generic:
function wrap<T>(x: T): T[]— caller infersT. - unknown: Safer than
any. Narrow before use. - as const: Literal types instead of widened
string. - tsconfig:
strict: true,"moduleResolution": "bundler"for Vite/Next.
Copy-paste examples
Function boundary and a discriminated union
Annotate what you export. Infer locals. Discriminated unions beat optional boolean pairs.
export interface User {
id: number;
email: string;
}
export type LoadState =
| { status: 'idle' }
| { status: 'loading' }
| { status: 'ok'; user: User }
| { status: 'err'; message: string };
export function label(state: LoadState): string {
switch (state.status) {
case 'ok':
return state.user.email;
case 'err':
return state.message;
default:
return '…';
}
}Common mistakes
anyon every API response because you did not write an interface.- Using
asassertions to silence errors instead of narrowing. enumfor every constant union when a string literal union would serialize cleaner.- Shipping
tscskipLibCheckplus broken types and blaming the language.
FAQ
- Does TypeScript run in the browser? No. It compiles to JavaScript. Source maps help you debug the TS you wrote.
- interface vs type? Interfaces merge and describe objects well. Type aliases handle unions and mapped types. Both are fine for props.
- Do I need classes? No. Functions plus interfaces cover most app code. Classes are useful for framework plugins and native-ish APIs.
Related TypeScript cheat sheets
- Typescript Types Interfaces Cheat Sheet
- Typescript Generics Cheat Sheet
- Typescript For React Cheat Sheet
Build with this stack
When a cheat sheet is not enough — you need a production app, a student FYP, or a custom dashboard — ArpaNeuro builds custom web development and also sells ready-made source code. Request a quote and tell us the stack.
Browse software development services or the source code marketplace if you want a working codebase instead of starting from a blank file.