# 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.

&#x20;`<type1,type2...typeN>` içerisine yapacağımız tip tanımlamaları ile generic tip tanımlamaları yapabiliriz.

Örneklerle açıklamaya çalışalım.

```typescript
function displayUser<T, U>(name: T, id: U) {
    console.log(`${name} ${id}`)
}

displayUser<string, string>("John", "1");

```

Fonksiyonumuzun isminin hemen yanında `<T,U>` ş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<T, U>(name: T, id: U) {
    console.log(`${name} ${id}`)
}

displayUser<string, number>("John", 1);
```

�Başka bir örnekte [interface](/typescript-notlari/handbook/interfaces.md) ve [type](/typescript-notlari/handbook/type.md) ile generic kullanımını inceleyebilirsiniz.

```typescript
interface Fullname<T, U> {
    (name: T, surname: U):void
}

type Name = "ahmet" | "fatih";
type Surname = "yavasi";

const getFullName: Fullname<Name, Surname> = (name: Name, surname: Surname) => {
    console.log(`${name} + ${surname}`)
}

getFullName('ahmet','yavasi')
```

�


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://afatihyavasi.gitbook.io/typescript-notlari/handbook/generics.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
