Support Online
Skip to main content

How to Create Custom Types in TypeScript?

🎯 What Will You Learn in This Guide?

In this guide, you will learn how to define your own data structures with the type keyword in TypeScript,
how to combine types with operators union (|) and intersection (&),
and how you can convert existing types with auxiliary types such as Record, Pick, Omit, Partial.
You'll also see modern TypeScript features like ? (optional features), indexable types (dynamic fields), and template literal types with hands-on examples.

🔍 1. Basics of Special Types

TypeScript introduces the type keyword to clearly and securely define complex data structures.
This makes the code more understandable, documentable and fault-tolerant.

🔸 Basic Type Definition

type Programci = {
ad: string;
uzmanlikAlanlari: string[];
};

This type defines an object with the name and specializationFields properties.


const ada: Programci = {
ad: 'Ada Lovelace',
uzmanlikAlanlari: ['Matematik', 'Bilişim', 'İlk Programcı']
};

The compiler immediately throws an error if there are missing or mistyped fields.


🧩 2. Optional Features

Some fields do not always need to be present. For this ? is used.


type Programci = {
ad: string;
uzmanlikAlanlari?: string[];
};

const ahmet: Programci = {
ad: 'Ahmet Yılmaz'
};

The specializationAreas field is now optional.


⚙️ 3. Extensible Types (Indexable Types)

Index signature is used in cases where the number of keys is not known in advance.


type AyarVerisi = {
[anahtar: string]: any;
};

const sunucuAyar: AyarVerisi = {
sunucuID: 'tr1-node01',
aktif: true,
port: 8080
};

This is useful for modeling API responses or dynamic configuration objects.


🔗 4. Union & Intersection

🧱 Union Types

Specifies that a value can be one of more than one type.


type UrunKodu = number | string;

const kod1: UrunKodu = 'TR1024';
const kod2: UrunKodu = 1024;
⚓ Kesişim Tipleri (Intersection)

It combines multiple types and requires having all the features.


type ApiYanit = { durumKodu: number; gecerli: boolean; };
type Kullanici = { ad: string; };

type KullaniciYaniti = ApiYanit & Kullanici;

let cevap: KullaniciYaniti = {
durumKodu: 200,
gecerli: true,
ad: 'GenixNode'
};

🧵 5. Template Literal Types

This feature, introduced with TypeScript 4.1, allows defining strings in a specific format.


type SunucuID = `genixnode_${number}`;

const sunucu: SunucuID = 'genixnode_500';
// const hatali: SunucuID = 'tr_500'; // ❌ Hata verir

🧰 6. Utility Types

🧩 Type💡 Description🔧 Usage
Partial<T>Makes all features in T optional.In update operations
Pick<T, K>Creates a new type by selecting certain keys.Subtyping
Omit<T, K>Excludes certain areas.Hiding sensitive data
Record<K, T>Creates a dictionary of key-value type.Dynamic data matching

examples


type Kullanici = { id: number; ad: string; eposta: string };

// 1️⃣ Partial
type KullaniciGuncelle = Partial<Kullanici>;

// 2️⃣ Pick
type KullaniciBilgisi = Pick<Kullanici, 'ad' | 'eposta'>;

// 3️⃣ Omit
type KullaniciDetay = Omit<Kullanici, 'id'>;

// 4️⃣ Record
type Metrikler = Record<string, number>;
const sunucuMetrikleri: Metrikler = { cpu: 75, ram: 4096 };

💬 Frequently Asked Questions (FAQ)

  1. What is the difference between type and interface?

interface is for extensible structures; type is more flexible in combination or conversion scenarios. In modern TS they can often be used together.

  1. Why should I use custom type instead of any?

any completely removes type safety. Custom types reduce the risk of errors at the compilation stage.

  1. What is the difference between Partial and Required?

Partial<T> makes all fields optional, Required<T> makes them all mandatory.

  1. Which settings should I turn on for large projects?

"strict": true must be active in tsconfig.json. Additionally, installing linter control with typescript-eslint improves quality.


🧭 Result

In this guide, you learned how to create, combine, and convert custom types with auxiliary types in TypeScript. Now you can increase type safety in your projects and make your data structures clearer and more maintainable.

💡 To test your app now: Deploy your own TypeScript project on GenixNode's powerful Node.js infrastructure and enjoy the scalable development experience 🚀