Avatar ·

خطأ "this" ضمنيًا له نوع "any" بسبب عدم وجود تعليق توضيحي للنوع في TypeScript

مرحبًا! بدأت تعلم TypeScript وواجهت خطأً لا أستطيع إصلاحه. لدي class بسيط يدير قائمة المهام:\n\n```typescript\nclass TaskList {\n tasks: string[] = [];\n\n addTask(task: string) {\n this.tasks.push(task);\n console.log(`Added: ${task}`);\n }\n\n printTasks() {\n this.tasks.forEach(function(task) {\n console.log(`${task} - added by ${this.name}`);\n });\n }\n}\n\nconst myList = new TaskList();\nmyList.addTask("Learn TypeScript");\nmyList.printTasks();\n```\n\nعند ترجمة هذا الكود، أحصل على الخطأ:\n\n```\n'this' implicitly has type 'any' because it does not have a type annotation\n```\n\nالخطأ يشير إلى السطر `console.log(`${task} - added by ${this.name}`);` داخل دالة `forEach`. أفهم أن `t