جلسهی 31: صفحه هم نوع دارد The Page Has Types
از این جلسه کد تایپاسکریپتت دوباره به صفحه دست میزند. میبینی هر چیزِ صفحه نوع دارد، چرا getElementById ممکن است null بدهد، و چطور به تایپاسکریپت بگویی یک کادر متن واقعاً کادر متن است.
در این جلسه یاد میگیری
- در پایان این جلسه میدانی جواب
document.getElementByIdاز نوعHTMLElement | nullاست و میتوانیnullرا کنار بگذاری. - در پایان این جلسه میدانی
HTMLElementچیست و چراvalueندارد. - در پایان این جلسه میتوانی با
querySelector<HTMLInputElement>یاasیک کادر متن را با نوع درستش بگیری. - در پایان این جلسه میدانی
valueهمیشه متن است و برای حساب باید باNumberعددش کنی.
درس
علی در جاوااسکریپت نوشته بود document.getElementById("lapm"). فقط دو حرف جابهجا! صفحه بدون هیچ خطایی باز شد. ولی با زدن دکمه هیچ اتفاقی نیفتاد: چیزی با این id پیدا نشده بود، پس جواب null (هیچ) بود، و null که classList ندارد. تایپاسکریپت این خطر را از قبل میداند. برای همین حتی وقتی id درست است، میگوید «شاید پیدا نشود». اجرا کن و خط قرمز را بخوان: lamp ممکن است null باشد.
<style>
body { font-family: sans-serif; text-align: center; padding: 16px; }
button, input, select { font-size: 16px; min-height: 40px; margin: 4px; }
.lamp { font-size: 80px; width: 130px; margin: 12px auto; border-radius: 24px; background: #334155; filter: grayscale(1); opacity: 0.5; }
.lamp.on { background: #fde047; filter: none; opacity: 1; }
</style>
<div id="lamp" class="lamp">💡</div>
<button id="go" onclick="press()">On</button>
<script type="text/typescript">
function press(): void {
const lamp = document.getElementById("lamp");
lamp.classList.add("on");
}
</script>
نوع جواب getElementById این است: HTMLElement | null. این همان «شاید هیچ» از جلسهی ۱۸ است، پس راهش را بلدی: اول با if مطمئن شو که پیدا شده. داخل if تایپاسکریپت میداند lamp دیگر null نیست و خط قرمز میرود. راه کوتاهتر هم هست: lamp?.classList.add("on"); یعنی «اگر بود، روشنش کن».
<style>
body { font-family: sans-serif; text-align: center; padding: 16px; }
button, input, select { font-size: 16px; min-height: 40px; margin: 4px; }
.lamp { font-size: 80px; width: 130px; margin: 12px auto; border-radius: 24px; background: #334155; filter: grayscale(1); opacity: 0.5; }
.lamp.on { background: #fde047; filter: none; opacity: 1; }
</style>
<div id="lamp" class="lamp">💡</div>
<button id="go" onclick="press()">On</button>
<script type="text/typescript">
function press(): void {
const lamp = document.getElementById("lamp");
if (lamp) {
lamp.classList.add("on");
}
}
</script>
HTMLElement یعنی «یک چیزِ صفحه»: لامپ، دکمه، تابلو، یک خط متن. هر HTMLElement اینها را دارد: classList، style و textContent. دو نکته در این کد هست. اول: if (!board) { return; } یعنی «اگر پیدا نشد، همینجا تمام کن». بعد از آن، تا آخر تابع board حتماً هست. دوم: textContent فقط متن میگیرد، پس عدد را با String(count) متن میکنیم. (با ?. نمیشود چیزی نوشت: board?.textContent = ... خط قرمز است.)
<style>
body { font-family: sans-serif; text-align: center; padding: 16px; }
button, input, select { font-size: 16px; min-height: 40px; margin: 4px; }
.board { font-size: 40px; width: 200px; max-width: 100%; margin: 12px auto; padding: 10px; border-radius: 12px; background: #e2e8f0; }
</style>
<div id="board" class="board">0</div>
<button id="go" onclick="press()">+1</button>
<script type="text/typescript">
let count: number = 0;
function press(): void {
const board = document.getElementById("board");
if (!board) {
return;
}
count = count + 1;
board.textContent = String(count);
}
</script>
حالا یک کادر متن داریم و میخواهیم نوشتهی داخلش را بخوانیم. null را هم کنار گذاشتهایم. ولی باز خط قرمز! اجرا کن: HTMLElement چیزی به اسم value ندارد. راست میگوید: لامپ و تابلو که value ندارند. فقط کادرهای ورودی دارند. getElementById فقط id را میبیند و نمیداند پشت آن یک لامپ است یا یک کادر متن.
<style>
body { font-family: sans-serif; text-align: center; padding: 16px; }
button, input, select { font-size: 16px; min-height: 40px; margin: 4px; }
</style>
<input id="name" type="text" placeholder="Your name">
<button id="go" onclick="press()">Hello</button>
<p id="msg"></p>
<script type="text/typescript">
function press(): void {
const box = document.getElementById("name");
const msg = document.getElementById("msg");
if (!box || !msg) {
return;
}
msg.textContent = "Hello, " + box.value;
}
</script>
کادر متن نوع خودش را دارد: HTMLInputElement. این نوع یک HTMLElement است بهعلاوهی value. با document.querySelector<HTMLInputElement>("#name") به تایپاسکریپت میگویی «چیزی که پیدا میکنی یک کادر ورودی است». این <...> همان جنریکِ فاز قبل است. جواب از نوع HTMLInputElement | null است، پس null را هنوز باید کنار بگذاری. یادت باشد در querySelector قبل از id یک # میآید.
<style>
body { font-family: sans-serif; text-align: center; padding: 16px; }
button, input, select { font-size: 16px; min-height: 40px; margin: 4px; }
</style>
<input id="name" type="text" placeholder="Your name">
<button id="go" onclick="press()">Hello</button>
<p id="msg"></p>
<script type="text/typescript">
function press(): void {
const box = document.querySelector<HTMLInputElement>("#name");
const msg = document.getElementById("msg");
if (!box || !msg) {
return;
}
msg.textContent = "Hello, " + box.value;
}
</script>
راه دوم as است: document.getElementById("name") as HTMLInputElement. as یعنی «به من اعتماد کن، این یک کادر ورودی است». با as دیگر null هم در کار نیست و if لازم نداری. ولی as مثل ! یک قول است: تایپاسکریپت بررسیاش نمیکند. اگر id را غلط نوشته باشی، برنامه موقع اجرا میایستد. پس فقط وقتی as بنویس که آن چیز را خودت در همین صفحه گذاشتهای و مطمئنی.
<style>
body { font-family: sans-serif; text-align: center; padding: 16px; }
button, input, select { font-size: 16px; min-height: 40px; margin: 4px; }
</style>
<input id="name" type="text" placeholder="Your name">
<button id="go" onclick="press()">Hello</button>
<p id="msg"></p>
<script type="text/typescript">
const box = document.getElementById("name") as HTMLInputElement;
const msg = document.getElementById("msg") as HTMLElement;
function press(): void {
msg.textContent = "Hello, " + box.value;
}
</script>
یک تلهی قدیمی: value همیشه متن است، حتی وقتی کادر از نوع number است و در آن 12 نوشتهای. در جاوااسکریپت box.value + 1 میشد 121. اینجا اگر بنویسی const age: number = box.value; خط قرمز میگیری، چون متن در جعبهی عددی جا نمیشود. راهش Number(box.value) است. یک سن بنویس و دکمه را بزن.
<style>
body { font-family: sans-serif; text-align: center; padding: 16px; }
button, input, select { font-size: 16px; min-height: 40px; margin: 4px; }
</style>
<input id="age" type="number" placeholder="Your age">
<button id="go" onclick="press()">Next year</button>
<p id="msg"></p>
<script type="text/typescript">
const box = document.getElementById("age") as HTMLInputElement;
const msg = document.getElementById("msg") as HTMLElement;
function press(): void {
const age: number = Number(box.value);
msg.textContent = "Next year: " + (age + 1);
}
</script>
خلاصه: getElementById میدهد HTMLElement | null. برای null سه راه داری: if، ?. (فقط برای خواندن و صدا زدن)، یا قولِ as. برای کادر ورودی نوع HTMLInputElement لازم است تا value داشته باشی. دکمه هم نوع خودش را دارد: HTMLButtonElement. اشتباه رایج: فکر کنی as چیزی را عوض میکند. as فقط حرف توست؛ در زمان اجرا هیچ کاری نمیکند.
مثالهای این جلسه
- مثال JavaScript: <button id="go" onclick="press()">On</butto…
- مثال JavaScript: <button id="go" onclick="press()">On</butto… — صفحه هم نوع دارد
- مثال JavaScript: <button id="go" onclick="press()">+1</butto…
- مثال JavaScript: <button id="go" onclick="press()">Hello</bu…
- مثال JavaScript: <button id="go" onclick="press()">Hello</bu… — صفحه هم نوع دارد
- مثال JavaScript: <button id="go" onclick="press()">Hello</bu… — صفحه هم نوع دارد 2
- مثال JavaScript: <button id="go" onclick="press()">Next year…
چالشها
- Maybe No Lamp (آسان)
- The Counter (آسان)
- Read the Field (متوسط)
- Double It (متوسط)
- Find the Red Lines (سخت)
- The Price Tag (سخت)
بیشتر بخوان
← جلسهی 30: پروژه: انبارِ همهچیز جلسهی 32: رویدادها با نوع →