Avatar ·

خطأ Property has no initializer في TypeScript: كيف أصلحه؟

مرحباً بالجميع! أنا مبتدئ في TypeScript و Angular، وقد واجهت خطأ غريباً جداً. لدي مكون (component) أعلن فيه خاصية (property)، لكن TypeScript يشتكي من أنها غير مهيأة (not initialized). إليك الكود الخاص بي: ```typescript import { Component, Input } from '@angular/core'; @Component({ selector: 'app-user', template: `

{{ user.name }}

{{ user.email }}

` }) export class UserComponent { @Input() user: User; public userName: string; private id: number; constructor() { // لا أفعل شيئاً في المُنشئ (constructor) } ngOnInit() { this.userName = this.user.name; this.id = this.user.id; } } ``` عند التجميع (compilation)، أحصل على أخطاء: - `Property 'user' has no initializer and is not definitely assigned in the constructor.` - `Property 'userName' has no initializer and is not definitely assigned in the constructor.` - `Property 'id' has no initializer and is not definitely assigned in th` }