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

جلسه‌ی 16: این یا آن Union Types

بعضی جعبه‌ها واقعاً دو جور مقدار می‌گیرند: گاهی عدد، گاهی متن. با | می‌گویی «این یا آن»، و با typeof می‌پرسی الان کدام‌یکی است.

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

درس

در مدرسه‌ی سارا شماره‌ی بعضی کمدها عدد است (7) و بعضی‌ها کد دارند ("A7"). سارا نوشته let locker: number. تا وقتی کمد عددی است همه چیز خوب است. ولی به‌محض این‌که کد "A7" را می‌گذارد، خط قرمز می‌گیرد. تایپ‌اسکریپت راست می‌گوید: خودش گفته بود «فقط عدد». ولی این بار اشتباه از نوع است، نه از مقدار.

<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">
let locker: number = 7;
show(locker);
locker = "A7";
show(locker);
</script>

راهش این است که راستش را بگویی: این جعبه عدد یا متن می‌گیرد. در تایپ‌اسکریپت «یا» را با | می‌نویسی: number | string. به این می‌گویند union، یعنی «این یا آن». حالا هر دو مقدار قبول می‌شود. ولی هنوز هر چیزی قبول نیست: locker = true; خط قرمز می‌گیرد، چون boolean در فهرست نیست.

<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">
let locker: number | string = 7;
show(locker);
locker = "A7";
show(locker);
</script>

ورودی یک تابع هم می‌تواند union باشد. حالا یک سؤال: داخل تابع، id عدد است یا متن؟ تایپ‌اسکریپت نمی‌داند. برای همین فقط کاری را اجازه می‌دهد که هر دو بلدند. toUpperCase() را فقط متن بلد است، عدد نه. پس خط قرمز می‌گیری: «شاید این عدد باشد، و عدد toUpperCase ندارد».

<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 shout(id: number | string): string {
  return id.toUpperCase();
}

show(shout("a7"));
</script>

پس باید اول بپرسی. typeof id === "string" یعنی «الان متن است؟». داخل آن if، تایپ‌اسکریپت مطمئن است که id متن است و toUpperCase() را اجازه می‌دهد. بعد از if هم خودش می‌فهمد فقط عدد مانده است. به این کار می‌گویند باریک کردن (narrowing): از «این یا آن» به «همین یکی» می‌رسی.

<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 shout(id: number | string): string {
  if (typeof id === "string") {
    return id.toUpperCase();
  }
  return "No. " + (id + 1);
}

show(shout("a7"));
show(shout(7));
</script>

یادت هست در جلسه‌ی اول "20" + 5 شد 205؟ بعضی وقت‌ها عدد واقعاً به شکل متن می‌رسد، مثلاً از یک فرم. با union و typeof می‌شود این را درست و بی‌خطر حل کرد: اگر متن بود، با 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">
function toNumber(value: number | string): number {
  if (typeof value === "string") {
    return Number(value);
  }
  return value;
}

show(toNumber("20") + 5);
show(toNumber(20) + 5);
</script>

آرایه هم می‌تواند از union باشد: (number | string)[] یعنی «آرایه‌ای که هر خانه‌اش عدد یا متن است». پرانتز مهم است: بدون آن، number | string[] یعنی «یک عدد، یا آرایه‌ای از متن» که چیز دیگری است. داخل حلقه برای هر خانه با typeof می‌پرسی.

<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 marks: (number | string)[] = [18, "15", 20];
let sum = 0;

for (const m of marks) {
  if (typeof m === "string") {
    sum = sum + Number(m);
  } else {
    sum = sum + m;
  }
}

show(sum);
</script>

اگر یک union را چند جا لازم داری، با type به آن اسم بده: type Id = number | string;. از آن به بعد همه‌جا فقط می‌نویسی Id. یک union می‌تواند بیشتر از دو عضو هم داشته باشد: number | string | boolean.

<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 Id = number | string;

function label(id: Id): string {
  return "ID: " + id;
}

const first: Id = 7;
const second: Id = "A7";
show(label(first));
show(label(second));
</script>

دو اشتباه رایج. اول: جواب typeof همیشه یک متن با حروف کوچک است: "string"، "number"، "boolean". اگر بنویسی "text" یا "String"، خط قرمز می‌گیری. دوم: union را بی‌دلیل بزرگ نکن. فقط نوع‌هایی را بنویس که واقعاً ممکن است برسند. هر چه فهرست کوتاه‌تر باشد، تایپ‌اسکریپت اشتباه‌های بیشتری را پیدا می‌کند.

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

چالش‌ها

  1. Number or Code (آسان)
  2. A Tag for Both (آسان)
  3. How Big Is It? (متوسط)
  4. Marks from a Form (متوسط)
  5. Find the Red Lines (سخت)
  6. Three in One (سخت)

بیشتر بخوان

← جلسه‌ی 15: پروژه: فروشگاه بازی جلسه‌ی 17: فقط همین مقدارها →