جلسهی 14: ساختن روی یک شکل Building on a Shape
یاد میگیری شکل تازه را روی شکل قبلی بسازی، بدون کپی کردن فیلدها: با extends برای interface و با & برای type. آخر هم میفهمی کجا interface بنویسی و کجا type.
در این جلسه یاد میگیری
- در پایان این جلسه میتوانی با
extendsیکinterfaceرا روی دیگری بسازی. - در پایان این جلسه میدانی جایی که شکل پدر لازم است، شکل فرزند هم قبول میشود.
- در پایان این جلسه میتوانی با
&دو شکل را یکی کنی. - در پایان این جلسه میدانی برای شکل شیء
interfaceمینویسیم و برای بقیهی اسمگذاریهاtype.
درس
در بازی رضا هم قهرمان هست، هم هیولا. هر دو اسم و جان (hp) دارند، پس رضا فیلدها را کپی کرده است. برنامه کار میکند. ولی فردا میخواهد به همه level بدهد: به Hero اضافه میکند و Monster را یادش میرود. کپی کردن یعنی هر تغییر را باید چند جا بدهی، و یک جا همیشه جا میماند.
<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 Hero {
name: string;
hp: number;
power: string;
}
interface Monster {
name: string;
hp: number;
teeth: number;
}
const h: Hero = { name: "Mina", hp: 30, power: "ice" };
const m: Monster = { name: "Grok", hp: 50, teeth: 12 };
show(h.name, m.name);
</script>
راه بهتر: فیلدهای مشترک را یک بار در یک شکل بنویس (Character) و بقیه را روی آن بساز. interface Hero extends Character یعنی «Hero همهی فیلدهای Character را دارد، بهعلاوهی اینها». حالا اگر level را به Character اضافه کنی، همه با هم آن را میگیرند.
<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 Character {
name: string;
hp: number;
}
interface Hero extends Character {
power: string;
}
interface Monster extends Character {
teeth: number;
}
const h: Hero = { name: "Mina", hp: 30, power: "ice" };
const m: Monster = { name: "Grok", hp: 50, teeth: 12 };
show(h.name, h.hp, h.power);
show(m.name, m.hp, m.teeth);
</script>
فیلدهای پدر شوخی نیستند: یک Hero باید همه را داشته باشد، هم مال خودش و هم مال Character. اینجا hp جا افتاده است. اجرا کن: خط قرمز میگوید hp لازم است، با اینکه در خود Hero اسمی از آن نیست.
<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 Character {
name: string;
hp: number;
}
interface Hero extends Character {
power: string;
}
const h: Hero = { name: "Mina", power: "ice" };
show(h.name);
</script>
یک فایدهی بزرگ: تابعی که Character میخواهد، Hero و Monster را هم قبول میکند. چرا؟ چون هر چه تابع لازم دارد (name و hp) در آنها هست. پس یک تابع hit برای همه کافی است. ولی داخل hit فقط فیلدهای Character را میشود دید، نه power را.
<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 Character {
name: string;
hp: number;
}
interface Hero extends Character {
power: string;
}
interface Monster extends Character {
teeth: number;
}
function hit(c: Character): string {
c.hp = c.hp - 10;
return c.name + " has " + c.hp + " hp";
}
const h: Hero = { name: "Mina", hp: 30, power: "ice" };
const m: Monster = { name: "Grok", hp: 50, teeth: 12 };
show(hit(h));
show(hit(m));
</script>
با type هم میشود شکلها را روی هم ساخت. علامتش & است، یعنی «و». type Player = Named & Scored; یعنی «یک Player هم Named است و هم Scored»، پس باید فیلدهای هر دو را داشته باشد.
<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 Named = { name: string };
type Scored = { score: number };
type Player = Named & Scored;
const p: Player = { name: "Sara", score: 10 };
show(p.name, p.score);
</script>
«و» یعنی هر دو، نه یکی از دو تا. این شیء فقط اسم دارد، پس Named هست ولی Scored نیست. اجرا کن: خط قرمز میگوید score جا افتاده است. اشتباه رایج دیگر: extends آخرش s دارد. interface Hero extend Character را تایپاسکریپت نمیتواند بخواند.
<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 Named = { name: string };
type Scored = { score: number };
type Player = Named & Scored;
const p: Player = { name: "Sara" };
show(p.name);
</script>
پس کی interface و کی type؟ برای شکل یک شیء هر دو کار میکنند. قرار ما در این دوره: شکل شیء → interface (و برای ساختن روی آن، extends). بقیهی اسمگذاریها → type، چون interface فقط شیء بلد است. مثلاً اسم دادن به یک جفت یا به نوع یک تابع، که در فاز قبل دیدی، فقط با type میشود.
<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;
score: number;
}
type Pair = [string, number];
type Rule = (p: Player) => boolean;
const pair: Pair = ["Sara", 10];
const isWinner: Rule = (p) => p.score >= 10;
const sara: Player = { name: pair[0], score: pair[1] };
show(sara.name, isWinner(sara));
</script>
مثالهای این جلسه
- مثال JavaScript: <script type="text/typescript"> — ساختن روی یک شکل
- مثال JavaScript: <script type="text/typescript"> — ساختن روی یک شکل 2
- مثال JavaScript: <script type="text/typescript"> — ساختن روی یک شکل 2 3
- مثال JavaScript: <script type="text/typescript"> — ساختن روی یک شکل 2 3 4
- مثال JavaScript: <script type="text/typescript"> — ساختن روی یک شکل 2 3 4 5
- مثال JavaScript: <script type="text/typescript"> — ساختن روی یک شکل 2 3 4 5 6
- مثال JavaScript: <script type="text/typescript"> — ساختن روی یک شکل 2 3 4 5 6 7
چالشها
- A Bird Is an Animal (آسان)
- The Cookie Has a Price (آسان)
- This and That (متوسط)
- The Copied Truck (متوسط)
- Find the Red Lines (سخت)
- The School (سخت)