Only with an id چالش 1 از جلسهی 27، آسان
تابع label برای هر چیزی یک برچسب میسازد: # و بعد شمارهی آن. ولی روی item.id خط قرمز است، چون T هیچ شرطی ندارد.
برای T شرط بگذار: باید یک id عددی داشته باشد.
برنامه باید #7 و بعد #3 را نشان بدهد.
<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 label<T>(item: T): string {
return "#" + item.id;
}
show(label({ id: 7, name: "Sara" }));
show(label({ id: 3, title: "Matilda" }));
</script>
راهنما و جواب این چالش · برگشت به درس جلسهی 27: با شرط · چالش بعدی: The Name of a Field
بیشتر بخوان: جاوااسکریپت چیست؟ از دکمهی وب تا Node.js