TypeScript Best Practices 2024

Yazılım 📖 1 dk okuma
#typescript#javascript#web#design

TypeScript, JavaScript’e tip güvenliği ekleyerek daha sağlam uygulamalar geliştirmemizi sağlar.

Strict Mode Kullanın

tsconfig.json dosyanızda strict mode’u aktif edin:

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

Type vs Interface

Interface’ler genişletilebilir, type’lar daha esnek:

// Interface - Extend edilebilir
interface User {
  name: string;
  email: string;
}

interface Admin extends User {
  role: 'admin';
}

// Type - Union ve intersection
type Status = 'active' | 'inactive' | 'pending';
type UserWithStatus = User & { status: Status };

Generic Kullanımı

Type-safe fonksiyonlar için generic’ler kullanın:

function getFirstElement<T>(arr: T[]): T | undefined {
  return arr[0];
}

TypeScript ile tip güvenli ve sürdürülebilir kod yazın!