TypeScript Best Practices

Hero image for article

Introduction

TypeScript has become the standard for building scalable JavaScript applications. This guide covers essential best practices for writing clean, type-safe TypeScript code.

Type Safety First

Always leverage TypeScript’s type system to catch errors at compile time:

// Good: Explicit types
interface User {
  id: string
  name: string
  email: string
}

function getUser(id: string): User {
  // Implementation
}

// Avoid: Using 'any'
function processData(data: any) {
  // Type safety lost
}

Use Strict Mode

Enable strict mode in your tsconfig.json:

{
  "compilerOptions": {
    "strict": true,
    "noImplicitAny": true,
    "strictNullChecks": true
  }
}

Utility Types

TypeScript provides powerful utility types:

// Partial - makes all properties optional
type PartialUser = Partial<User>

// Pick - select specific properties
type UserPreview = Pick<User, 'name' | 'email'>

// Omit - exclude specific properties
type UserWithoutId = Omit<User, 'id'>

Discriminated Unions

Use discriminated unions for type-safe state management:

type LoadingState =
  | { status: 'idle' }
  | { status: 'loading' }
  | { status: 'success'; data: User[] }
  | { status: 'error'; error: string }

function renderState(state: LoadingState) {
  switch (state.status) {
    case 'loading':
      return 'Loading...'
    case 'success':
      return state.data.map(user => user.name)
    case 'error':
      return state.error
    default:
      return null
  }
}

Generic Constraints

Use generic constraints to create flexible, type-safe functions:

function getProperty<T, K extends keyof T>(obj: T, key: K): T[K] {
  return obj[key]
}

const user: User = { id: '1', name: 'John', email: 'john@example.com' }
const name = getProperty(user, 'name') // Type: string

Conclusion

Following these TypeScript best practices will help you write more maintainable, error-free code. Start with strict mode, leverage the type system, and use advanced features like discriminated unions and generics.

Share this article

Found this helpful? Share it with others who might benefit.

Other things I've written