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

جلسه‌ی 17: فقط همین مقدارها Exact Values

string یعنی هر متنی، حتی متنی که غلط تایپ شده. با نوع‌های دقیق مثل "easy" | "hard" می‌گویی فقط همین چند مقدار قبول است، و غلط تایپی خط قرمز می‌شود.

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

درس

رضا یک بازی نوشته است. در سطح easy دشمن‌ها آهسته‌اند (سرعت ۱) و در سطح hard تند (سرعت ۳). مینا می‌خواهد آسان بازی کند، ولی رضا هنگام صدا زدن تابع دستش می‌لغزد و می‌نویسد "ezy". اجرا کن: هیچ خطایی نیست، ولی سرعت شد 3! چون "ezy" هم یک string است و تایپ‌اسکریپت چیزی برای ایراد گرفتن ندارد.

<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">
function speed(level: string): number {
  if (level === "easy") {
    return 1;
  }
  return 3;
}

show(speed("easy"));
show(speed("ezy"));
</script>

مشکل این است که string خیلی بزرگ است: هر متنی را قبول می‌کند. ولی سطح بازی فقط دو مقدار دارد. در تایپ‌اسکریپت خودِ یک مقدار هم می‌تواند نوع باشد: نوع "easy" یعنی «فقط و فقط متن easy». به این می‌گویند نوع دقیق (literal type). با | چند تا را کنار هم می‌گذاری: type Level = "easy" | "hard"; یعنی «یا easy یا hard، نه هیچ چیز دیگر».

<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 Level = "easy" | "hard";

function speed(level: Level): number {
  if (level === "easy") {
    return 1;
  }
  return 3;
}

show(speed("easy"));
show(speed("hard"));
</script>

حالا همان لغزش دست را تکرار می‌کنیم. اجرا کن: این بار برنامه اجرا نمی‌شود. خط قرمز می‌گوید "ezy" در فهرست Level نیست. اشتباهی که قبلاً بی‌صدا بازی را سخت می‌کرد، حالا قبل از اجرا پیدا شد. یک خبر خوب دیگر: در ویرایشگرهای واقعی، وقتی گیومه را باز می‌کنی، خودش مقدارهای مجاز را پیشنهاد می‌دهد.

<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 Level = "easy" | "hard";

function speed(level: Level): number {
  if (level === "easy") {
    return 1;
  }
  return 3;
}

show(speed("ezy"));
</script>

یک نکته‌ی ظریف. در جلسه‌ی ۳ دیدی که تایپ‌اسکریپت نوع را حدس می‌زند. برای let choice = "easy"; حدسش string است، چون let بعداً می‌تواند هر متنی بگیرد. و string را نمی‌شود جای Level داد: «شاید متن دیگری باشد». برای همین این کد خط قرمز دارد. راهش این است که نوع را خودت بنویسی: let choice: Level = "easy";

<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 Level = "easy" | "hard";

function speed(level: Level): number {
  if (level === "easy") {
    return 1;
  }
  return 3;
}

let choice = "easy";
show(speed(choice));
</script>

با === می‌توانی نوع دقیق را هم باریک کنی، درست مثل typeof در جلسه‌ی قبل. این‌جا سه سطح داریم. بعد از دو if اول، تایپ‌اسکریپت می‌داند فقط "hard" مانده است. اگر در یک if مقداری بنویسی که در فهرست نیست (مثلاً level === "medum")، همان‌جا خط قرمز می‌گیری.

<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 Level = "easy" | "medium" | "hard";

function lives(level: Level): number {
  if (level === "easy") {
    return 5;
  }
  if (level === "medium") {
    return 3;
  }
  return 1;
}

show(lives("easy"));
show(lives("medium"));
show(lives("hard"));
</script>

نوع دقیق فقط برای متن نیست. عدد هم می‌تواند دقیق باشد. تاس فقط شش مقدار دارد: type Dice = 1 | 2 | 3 | 4 | 5 | 6;. حالا isLucky(7) خط قرمز می‌گیرد، چون تاس ۷ ندارد. با number این اشتباه رد می‌شد.

<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 Dice = 1 | 2 | 3 | 4 | 5 | 6;

function isLucky(roll: Dice): boolean {
  return roll === 6;
}

show(isLucky(6));
show(isLucky(2));
</script>

نوع دقیق در فیلد یک شکل هم خیلی به کار می‌آید. این‌جا هر بازیکن در تیم red یا blue است. اگر کسی بنویسد team: "green"، خط قرمز می‌گیرد. شکل داده‌ات حالا دقیقاً همان چیزی را می‌گوید که در بازی ممکن است.

<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 {
  name: string;
  team: "red" | "blue";
}

const sara: Player = { name: "Sara", team: "red" };
const ali: Player = { name: "Ali", team: "blue" };
show(sara.name, sara.team);
show(ali.name, ali.team);
</script>

اشتباه رایج: حروف بزرگ و کوچک فرق دارند. "Easy" و "easy" دو مقدار جدا هستند و اگر فهرست فقط "easy" دارد، "Easy" خط قرمز می‌گیرد. یک قانون ساده: هر جا دیدی مقدارهای ممکن چند تای مشخص هستند (سطح بازی، جهت، رنگ تیم، روز هفته)، به‌جای string یک union از مقدارهای دقیق بنویس.

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

چالش‌ها

  1. Fix the Red Line (آسان)
  2. Heads or Tails (آسان)
  3. Four Directions (متوسط)
  4. Three Stars (متوسط)
  5. Find the Red Lines (سخت)
  6. The Pizza Shop (سخت)

بیشتر بخوان

← جلسه‌ی 16: این یا آن جلسه‌ی 18: شاید هیچ →