Why TypeScript in 2026?
TypeScript adoption continues to grow. Over 85% of large-scale JavaScript projects now use TypeScript. These patterns will help you write better, safer code.
1. Use Discriminated Unions
type ApiResult<T> =
| { status: "success"; data: T }
| { status: "error"; code: number; message: string }
| { status: "loading" };
function render<T>(result: ApiResult<T>) {
switch (result.status) {
case "success": return result.data; // T is inferred
case "error": return result.message; // string is inferred
case "loading": return "Loading...";
}
}
2. Utility Types You Must Know
interface User {
id: number;
name: string;
email: string;
password: string;
role: "admin" | "user";
}
// Pick only what you need
type PublicUser = Pick<User, "id" | "name" | "email">;
// Omit sensitive fields
type SafeUser = Omit<User, "password">;
// Make all fields optional
type UserUpdate = Partial<User>;
// Make all fields required
type StrictUser = Required<UserUpdate>;
// Make all fields read-only
type ImmutableUser = Readonly<User>;
3. Template Literal Types
type HttpMethod = "GET" | "POST" | "PUT" | "DELETE" | "PATCH";
type ApiEndpoint = "/users" | "/products" | "/orders";
type ApiRoute = `${HttpMethod} ${ApiEndpoint}`;
// "GET /users" | "GET /products" | "POST /users" | ... all combinations!
4. Satisfies Operator (TS 4.9+)
const config = {
host: "localhost",
port: 3000,
debug: true,
} satisfies Record<string, string | number | boolean>;
// config.port is still typed as number (not string | number | boolean)
const doubled = config.port * 2; // ✅ Works!
The
satisfiesoperator validates a type without widening it — you get both type safety AND precise inference.
5. Mapped Types
// Make all methods async
type AsyncMethods<T> = {
[K in keyof T]: T[K] extends (...args: infer A) => infer R
? (...args: A) => Promise<R>
: T[K];
};
interface UserService {
getUser(id: number): User;
deleteUser(id: number): boolean;
}
type AsyncUserService = AsyncMethods<UserService>;
// getUser(id: number): Promise<User>
// deleteUser(id: number): Promise<boolean>
Summary
TypeScript is more than just “JavaScript with types.” These patterns showcase its power as a full type-level programming language. Start applying them today to write code that is self-documenting, refactoring-safe, and truly enjoyable to maintain.