TypeScript Cheat Sheet
This quick reference guide covers common TypeScript types, built-in utility types, and typical configurations for daily use.
1. Type Annotations Quick Guide
Here is a quick look at defining standard structures:
// Primitive Types
const message: string = "hello";
const score: number = 42;
// Array Types
const list: number[] = [1, 2, 3];
const names: Array<string> = ["Alice", "Bob"];
// Tuple Types
const coordinates: [x: number, y: number] = [100, 200];
// Union Types
const input: string | number = "test";2. Interface vs. Type Alias Comparison
// Object Structure (Either interface or type alias works)
interface Person {
name: string;
}
type Employee = {
id: number;
};
// Combining Shapes
interface Manager extends Person {
department: string;
}
type Director = Person & Employee & {
level: number;
};3. Built-in Utility Types
interface User {
id: string;
name: string;
email: string;
}
// Make all properties optional
type UpdateUser = Partial<User>;
// Make all properties mandatory
type FullUser = Required<UpdateUser>;
// Make all properties read-only
type ReadonlyUser = Readonly<User>;
// Pick select properties
type SimpleUser = Pick<User, "name" | "email">;
// Remove select properties
type SecureUser = Omit<User, "id">;4. Compiler Settings Cheat Sheet
Use these core compiler options in your tsconfig.json file:
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true
}
}Keep "strict": true enabled to catch the maximum number of potential issues during compilation.
Published on Last updated: