TypeScript6 dk
TypeScript ile Daha Güvenli Kod Yazın
#TypeScript#JavaScript#Best Practices
TypeScript ile Daha Güvenli Kod Yazın
TypeScript, JavaScript'e static typing ekleyerek kod kalitesini artırır. İşte pratik ipuçları:
Type vs Interface
// Type - Union ve primitive'ler için type ID = string | number; type Status = 'pending' | 'success' | 'error'; // Interface - Object yapıları için interface User { id: ID; name: string; email: string; } // Interface extends edilebilir interface Admin extends User { permissions: string[]; }
Utility Types
TypeScript'in built-in utility type'ları çok güçlü:
interface User { id: string; name: string; email: string; password: string; } // Partial - Tüm field'ları optional yapar type UpdateUser = Partial<User>; // Pick - Sadece belirli field'ları seç type UserPreview = Pick<User, 'id' | 'name'>; // Omit - Belirli field'ları çıkar type PublicUser = Omit<User, 'password'>; // Readonly - Tüm field'ları readonly yapar type ImmutableUser = Readonly<User>;
Generics ile Reusable Functions
// Generic API response interface ApiResponse<T> { data: T; status: number; message: string; } async function fetchData<T>(url: string): Promise<ApiResponse<T>> { const response = await fetch(url); return response.json(); } // Kullanım const users = await fetchData<User[]>('/api/users'); const profile = await fetchData<User>('/api/profile');
Type Guards
function isUser(obj: any): obj is User { return 'id' in obj && 'name' in obj && 'email' in obj; } function processData(data: unknown) { if (isUser(data)) { // TypeScript artık data'nın User olduğunu biliyor console.log(data.name); } }
Enum Alternatifleri
// Enum yerine const object kullanın const UserRole = { ADMIN: 'admin', USER: 'user', GUEST: 'guest' } as const; type UserRole = typeof UserRole[keyof typeof UserRole];
Best Practices
anyyerineunknownkullanın- Strict mode'u aktif edin
- Type assertion'dan kaçının
- Complex type'lar için type alias oluşturun
- API response'ları için interface tanımlayın
TypeScript, kodunuzu daha güvenli ve maintainable hale getirir!