CodeBlanks Logo Icon CodeBlanks

Partial

Learn to make all properties optional with Partial.

Partial

Partial<T> makes every property of T optional. It behaves as if each property got a ?; properties you leave out can be undefined. It is ideal for updates or partial data flows where only some fields of an object arrive.

interface Kullanici {
  ad: string;
  yas: number;
}
function guncelle(u: Partial<Kullanici>) {
  // u.ad and u.yas optional
}

The partial update pattern

Partial shines in update functions. When the parameter is Partial<Kullanici>, the caller supplies only the fields they want to change. The body skips missing fields with an undefined check. This keeps calls concise without losing type safety.

Practice Partial update

Define interface Kullanici with ad: string and yas: number. Write function goster(u: Partial<Kullanici>): void; in the body, if u.ad is present, print it with console.log (otherwise skip). Call goster({ ad: "Deniz" }).

Starter code

index.ts
// Kullanici arayüzünü ad: string ve yas: number ile tanımla
// goster fonksiyonu parametresi Kullanici'nın kısmi hali olsun
// gövdede u.ad varsa console.log ile yazdır
// { ad: "Deniz" } nesnesiyle çağır

Sample solution

index.ts
interface Kullanici {
  ad: string;
  yas: number;
}
function goster(u: Partial<Kullanici>): void {
  if (u.ad) {
    console.log(u.ad);
  }
}
goster({ ad: "Deniz" });

Expected output

Deniz

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What does <code>Partial&lt;Kullanici&gt;</code> provide?

index.ts
interface Kullanici { ad: string; yas: number; }
function guncelle(u: Partial<Kullanici>) {}
  • Makes all properties optional Correct answer
  • Makes all properties required
  • Removes some properties
  • Makes all properties readonly

Explanation: Partial adds ? to every property, making them all optional. Required makes them required, Omit removes them, Readonly makes them readonly.

2 Add Partial

Question
interface Ayar { tema: string; ses: boolean; }
function ayarla(a: ____<Ayar>) {
  // a.tema ve a.ses isteğe bağlı
}
Answer
interface Ayar { tema: string; ses: boolean; }
function ayarla(a: Partial<Ayar>) {
  // a.tema ve a.ses isteğe bağlı
}

Explanation: To make all properties optional you use Partial. Required does the opposite; Pick selects properties; Omit removes them.

3 Partial parts

Term Meaning
Partial<T> Makes all properties optional
Partial<Kullanici> Use on a type
u.ad? Result: optional property
guncelle({ yas: 30 }) Call with partial data

All lessons in Intermediate