Partial
- TypeScript course
- Intermediate
- Lesson 24/89
- 4 min
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
// 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ğırSample solution
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<Kullanici></code> provide?
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
interface Ayar { tema: string; ses: boolean; }
function ayarla(a: ____<Ayar>) {
// a.tema ve a.ses isteğe bağlı
}
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 |