TypeScript Best Practices for 2025

TypeScript has matured significantly. These patterns will help you write safer, cleaner, and more maintainable code.

Strict Configuration

Start every project with strict mode:

json
{
"compilerOptions": {
"strict": true,
"noUncheckedIndexedAccess": true,
"exactOptionalPropertyTypes": true,
"noFallthroughCasesInSwitch": true,
"forceConsistentCasingInFileNames": true
}
}

Discriminated Unions Over Enums

Enums have runtime overhead and quirky behavior. Use discriminated unions instead:

typescript
// ❌ Avoid
enum Status { Loading, Success, Error }
// ✅ Prefer
type Status =
| { kind: 'loading' }
| { kind: 'success'; data: Post[] }
| { kind: 'error'; message: string };
function renderStatus(status: Status) {
switch (status.kind) {
case 'loading': return <Spinner />;
case 'success': return <PostList posts={status.data} />;
case 'error': return <Alert message={status.message} />;
}
}

Utility Types You Should Know

TypeDescriptionExample
Partial<T>
All properties optional
Partial<User>
Required<T>
All properties required
Required<Config>
Pick<T, K>
Select specific keys
Pick<User, 'id' | 'name'>
Omit<T, K>
Exclude specific keys
Omit<User, 'password'>
Record<K, V>
Key-value map
Record<string, number>
Extract<T, U>
Extract matching types
Extract<Event, { type: 'click' }>

Type Guards

Custom type guards make your code safer:

typescript
function isApiError(error: unknown): error is { message: string; code: number } {
return (
typeof error === 'object' &&
error !== null &&
'message' in error &&
'code' in error
);
}

satisfies Operator

The satisfies operator validates types without widening them:

typescript
const palette = {
red: [255, 0, 0],
green: '#00ff00',
blue: [0, 0, 255],
} satisfies Record<string, string | number[]>;
// palette.green is typed as string (not string | number[])
palette.green.toUpperCase(); // ✅ Works!

Summary

Modern TypeScript is about leveraging the type system to catch errors before they reach production. Use strict mode, prefer unions over enums, learn the utility types, and let the compiler be your first line of defense.