بتل کد Battle Codeدارد باز می‌شود…

جلسه‌ی 29: ابزارهای آماده Ready-made Tools

تایپ‌اسکریپت چند شکل جنریکِ آماده دارد که از یک شکل، شکل تازه می‌سازند: Partial (همه اختیاری)، Pick (فقط این‌ها)، Omit (همه به‌جز این‌ها)، Record (جدول) و Readonly (قفل). دیگر لازم نیست شکل‌ها را با دست کپی کنی.

در این جلسه یاد می‌گیری

درس

رضا یک شکل Player دارد. حالا تابعی می‌خواهد که فقط بعضی از فیلدهای بازیکن را عوض کند: گاهی فقط اسم، گاهی فقط امتیاز. پس یک شکل دوم با دست نوشت: PlayerChanges، همان فیلدها، ولی همه با ?. کار می‌کند. ولی فردا که به Player فیلد level اضافه شود، باید یادش بماند PlayerChanges را هم عوض کند. دو شکل که باید همیشه با هم جور بمانند: این همان کپیِ خطرناک است.

<style>
body { font-family: sans-serif; text-align: center; padding: 16px; }
button, input, select { font-size: 16px; min-height: 40px; margin: 4px; }
#out { direction: ltr; text-align: left; font: 16px monospace; margin: 12px 0; padding: 12px; min-height: 28px; border-radius: 12px; background: #0f172a; color: #e2e8f0; white-space: pre-wrap; overflow-wrap: anywhere; }
#out .line { min-height: 22px; }
</style>

<pre id="out"></pre>

<script type="text/typescript">
interface Player {
  id: number;
  name: string;
  score: number;
}

interface PlayerChanges {
  id?: number;
  name?: string;
  score?: number;
}

const changes: PlayerChanges = { score: 50 };
show(changes.score);
</script>

تایپ‌اسکریپت این کار را آماده دارد: Partial<Player>. یک فرم را تصور کن که لازم نیست همه‌ی خانه‌هایش را پُر کنی. Partial<Player> یعنی «همان Player، ولی همه‌ی فیلدها اختیاری». این‌جا تابع update یک بازیکن و چند تغییر می‌گیرد. { ...player, ...changes } یعنی «اول همه‌ی فیلدهای بازیکن، بعد تغییرها روی آن‌ها».

<style>
body { font-family: sans-serif; text-align: center; padding: 16px; }
button, input, select { font-size: 16px; min-height: 40px; margin: 4px; }
#out { direction: ltr; text-align: left; font: 16px monospace; margin: 12px 0; padding: 12px; min-height: 28px; border-radius: 12px; background: #0f172a; color: #e2e8f0; white-space: pre-wrap; overflow-wrap: anywhere; }
#out .line { min-height: 22px; }
</style>

<pre id="out"></pre>

<script type="text/typescript">
interface Player {
  id: number;
  name: string;
  score: number;
}

function update(player: Player, changes: Partial<Player>): Player {
  return { ...player, ...changes };
}

const sara: Player = { id: 1, name: "Sara", score: 10 };
const better = update(sara, { score: 50 });
show(better.name, better.score);
</script>

«اختیاری» یعنی می‌توانی فیلد را ننویسی. یعنی این نیست که هر چیزی بنویسی! این‌جا اسم فیلد غلط نوشته شده است: scor. اجرا کن: خط قرمز. Partial<Player> هنوز می‌داند Player چه فیلدهایی دارد و هر کدام چه نوعی است.

<style>
body { font-family: sans-serif; text-align: center; padding: 16px; }
button, input, select { font-size: 16px; min-height: 40px; margin: 4px; }
#out { direction: ltr; text-align: left; font: 16px monospace; margin: 12px 0; padding: 12px; min-height: 28px; border-radius: 12px; background: #0f172a; color: #e2e8f0; white-space: pre-wrap; overflow-wrap: anywhere; }
#out .line { min-height: 22px; }
</style>

<pre id="out"></pre>

<script type="text/typescript">
interface Player {
  id: number;
  name: string;
  score: number;
}

const changes: Partial<Player> = { scor: 50 };
show(changes.score);
</script>

ابزار دوم: Pick، یعنی «بردار». برای جدول امتیازها فقط اسم و امتیاز لازم است، نه id. Pick<Player, "name" | "score"> یعنی «از Player فقط name و score را بردار». اسم فیلدها را با | کنار هم می‌نویسی، مثل union. اگر اسمی بنویسی که در Player نیست، خط قرمز می‌گیری (یادت هست؟ keyof).

<style>
body { font-family: sans-serif; text-align: center; padding: 16px; }
button, input, select { font-size: 16px; min-height: 40px; margin: 4px; }
#out { direction: ltr; text-align: left; font: 16px monospace; margin: 12px 0; padding: 12px; min-height: 28px; border-radius: 12px; background: #0f172a; color: #e2e8f0; white-space: pre-wrap; overflow-wrap: anywhere; }
#out .line { min-height: 22px; }
</style>

<pre id="out"></pre>

<script type="text/typescript">
interface Player {
  id: number;
  name: string;
  score: number;
}

type ScoreRow = Pick<Player, "name" | "score">;

const row: ScoreRow = { name: "Ali", score: 30 };
show(row.name, row.score);
</script>

ابزار سوم برعکسِ Pick است: Omit، یعنی «جا بینداز». Omit<Player, "id"> یعنی «همه‌ی Player، به‌جز id». کجا به درد می‌خورد؟ بازیکن تازه هنوز شماره ندارد؛ شماره را برنامه می‌دهد. پس تابع create یک Omit<Player, "id"> می‌گیرد و خودش id را اضافه می‌کند.

<style>
body { font-family: sans-serif; text-align: center; padding: 16px; }
button, input, select { font-size: 16px; min-height: 40px; margin: 4px; }
#out { direction: ltr; text-align: left; font: 16px monospace; margin: 12px 0; padding: 12px; min-height: 28px; border-radius: 12px; background: #0f172a; color: #e2e8f0; white-space: pre-wrap; overflow-wrap: anywhere; }
#out .line { min-height: 22px; }
</style>

<pre id="out"></pre>

<script type="text/typescript">
interface Player {
  id: number;
  name: string;
  score: number;
}

let nextId: number = 1;

function create(data: Omit<Player, "id">): Player {
  const player: Player = { id: nextId, ...data };
  nextId = nextId + 1;
  return player;
}

const mina = create({ name: "Mina", score: 0 });
show(mina.id, mina.name);
</script>

ابزار چهارم: Record، یعنی یک جدول. Record<string, number> یعنی «جدولی که کلیدهایش متن‌اند و مقدارهایش عدد»، مثل دفتر نمره: اسم ← نمره. کلید می‌تواند یک union از مقدارهای دقیق هم باشد. در Record<Size, number> کلیدها فقط "small"، "medium" و "big" هستند.

<style>
body { font-family: sans-serif; text-align: center; padding: 16px; }
button, input, select { font-size: 16px; min-height: 40px; margin: 4px; }
#out { direction: ltr; text-align: left; font: 16px monospace; margin: 12px 0; padding: 12px; min-height: 28px; border-radius: 12px; background: #0f172a; color: #e2e8f0; white-space: pre-wrap; overflow-wrap: anywhere; }
#out .line { min-height: 22px; }
</style>

<pre id="out"></pre>

<script type="text/typescript">
const grades: Record<string, number> = {};
grades["Sara"] = 18;
grades["Ali"] = 16;
show(grades["Sara"] + grades["Ali"]);

type Size = "small" | "medium" | "big";
const prices: Record<Size, number> = { small: 10, medium: 20, big: 30 };
show(prices.medium);
</script>

خوبیِ Record<Size, number> این است که هیچ کلیدی جا نمی‌ماند. این‌جا قیمت medium فراموش شده است. اجرا کن: خط قرمز می‌گوید medium کم است. اگر روزی اندازه‌ی "huge" را به Size اضافه کنی، همه‌ی جدول‌هایی که قیمتش را ندارند قرمز می‌شوند.

<style>
body { font-family: sans-serif; text-align: center; padding: 16px; }
button, input, select { font-size: 16px; min-height: 40px; margin: 4px; }
#out { direction: ltr; text-align: left; font: 16px monospace; margin: 12px 0; padding: 12px; min-height: 28px; border-radius: 12px; background: #0f172a; color: #e2e8f0; white-space: pre-wrap; overflow-wrap: anywhere; }
#out .line { min-height: 22px; }
</style>

<pre id="out"></pre>

<script type="text/typescript">
type Size = "small" | "medium" | "big";

const prices: Record<Size, number> = { small: 10, big: 30 };
show(prices.small);
</script>

ابزار پنجم: Readonly. readonly را از جلسه‌ی ۱۲ می‌شناسی: فیلدی که عوض نمی‌شود. Readonly<Player> همه‌ی فیلدها را یک‌جا قفل می‌کند، مثل قاب شیشه‌ای موزه: نگاه کن، دست نزن. این‌جا کسی می‌خواهد امتیاز را عوض کند و خط قرمز می‌گیرد. یک نکته: همه‌ی این پنج ابزار خودشان شکل‌های جنریک‌اند، مثل Box<T>. فقط کسی قبلاً آن‌ها را برای تو نوشته است.

<style>
body { font-family: sans-serif; text-align: center; padding: 16px; }
button, input, select { font-size: 16px; min-height: 40px; margin: 4px; }
#out { direction: ltr; text-align: left; font: 16px monospace; margin: 12px 0; padding: 12px; min-height: 28px; border-radius: 12px; background: #0f172a; color: #e2e8f0; white-space: pre-wrap; overflow-wrap: anywhere; }
#out .line { min-height: 22px; }
</style>

<pre id="out"></pre>

<script type="text/typescript">
interface Player {
  id: number;
  name: string;
  score: number;
}

const champion: Readonly<Player> = { id: 1, name: "Sara", score: 100 };
champion.score = 0;
show(champion.score);
</script>

مثال‌های این جلسه

چالش‌ها

  1. A Half-filled Form (آسان)
  2. Lock the Settings (آسان)
  3. A Book Without a Number (متوسط)
  4. The Fruit Stand (متوسط)
  5. Find the Red Lines (سخت)
  6. The To-do List (سخت)

بیشتر بخوان

← جلسه‌ی 28: شکل‌های جنریک جلسه‌ی 30: پروژه: انبارِ همه‌چیز →