You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Generics
Dinamik ve yeniden kullanılabilir kod yazmak söz konusu olduğunda **Kendini Tekrar Etme \(DRY\)** ilkesine uymak önemlidir. **Generic**, **TypeScript** de bunu yapmamızı sağlar.
`` içerisine yapacağımız tip tanımlamaları ile generic tip tanımlamaları yapabiliriz.
Örneklerle açıklamaya çalışalım.
```typescript
function displayUser(name: T, id: U) {
console.log(`${name} ${id}`)
}
displayUser("John", "1");
```
Fonksiyonumuzun isminin hemen yanında `` şeklinde bir generic görüyoruz. **T** name tipini, **U** ise id tipini belirtmektedir. id tipini **number** yapmak istediğimizde tek yapmamız gereken fonksiyonumuzu çağırdığımız yerdeki generic tipini değiştirmektir.
```typescript
function displayUser(name: T, id: U) {
console.log(`${name} ${id}`)
}
displayUser("John", 1);
```
Başka bir örnekte [interface](interfaces.md) ve [type](type.md) ile generic kullanımını inceleyebilirsiniz.
```typescript
interface Fullname {
(name: T, surname: U):void
}
type Name = "ahmet" | "fatih";
type Surname = "yavasi";
const getFullName: Fullname = (name: Name, surname: Surname) => {
console.log(`${name} + ${surname}`)
}
getFullName('ahmet','yavasi')
```