CodeBlanks Logo Icon CodeBlanks

Required and Pick

Learn Required for required and Pick for selecting.

Required and Pick

Required<T> turns T's optional properties into required ones; it drops the ?. Pick<T, K> keeps only the properties of T listed in K and discards the rest. Together they narrow and tighten an interface.

interface Uye {
  ad: string;
  eposta?: string;
}
type ZorunluUye = Required<Uye>;
type SadeceAd = Pick<Uye, "ad">;

Required vs Partial

Required is the opposite of Partial. Fields left optional in an interface may become required for some functions. For example, if eposta is required at registration you use Required<Uye>. This lets you reuse the same base type at different strictness levels.

Practice Required member and summary

Define interface Uye with ad: string and eposta?: string. Write function kayit(u: Required<Uye>): void whose body prints the eposta via console.log(u.eposta). Call kayit({ ad: "Selin", eposta: "s@x.com" }).

Starter code

index.ts
// Uye arayüzünü ad: string ve eposta?: string ile tanımla
// kayit fonksiyonu parametresi Uye'nin zorunlu hali olsun
// gövdede console.log ile u.eposta yazdır
// ad ve eposta içeren nesneyle çağır

Sample solution

index.ts
interface Uye {
  ad: string;
  eposta?: string;
}
function kayit(u: Required<Uye>): void {
  console.log(u.eposta);
}
kayit({ ad: "Selin", eposta: "s@x.com" });

Expected output

s@x.com

Questions and answers

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

1 What does <code>Pick&lt;Uye, "ad" | "eposta"&gt;</code> produce?

index.ts
interface Uye { ad: string; eposta?: string; }
type T = Pick<Uye, "ad" | "eposta">;
  • A type with all properties optional
  • A type with ad and eposta removed
  • A type with all properties required
  • A type with only ad and eposta properties Correct answer

Explanation: Pick keeps the named properties and discards the rest. Optional is Partial, removal is Omit, required is Required.

2 Select properties with Pick

Question
interface Urun { ad: string; fiyat: number; stok: number; }
type Ozet = ____<Urun, "ad" | "fiyat">;
Answer
interface Urun { ad: string; fiyat: number; stok: number; }
type Ozet = Pick<Urun, "ad" | "fiyat">;

Explanation: To select specific properties you use Pick. Partial makes all optional; Required makes them required; Omit removes them.

3 Required and Pick parts

Term Meaning
Required<T> Makes optionals required
Pick<T, K> Selects properties in K
Pick<Uye, "ad"> Single property selection
Required<Uye> eposta becomes required

All lessons in Intermediate