티스토리 뷰

class Employee {
    fullName: string;
    age: number;
    jobTitle: string;

    details = ():void=>{
        console.log(`${this.fullName}의 나이는 ${this.age}이고, 직업은 ${this.jobTitle}입니다. `)
    }
}

 

이러한 클래스를 생성하면 

Property 'fullName' has no initializer and is not definitely assigned in the constructor.

Property 'age' has no initializer and is not definitely assigned in the constructor.

Property 'jobTitle' has no initializer and is not definitely assigned in the constructor.

 

이 문제를 해결하는 가장 간단한 방법은 프로퍼티에 !를 붙이면 됩니다. 코드에서 확인하세요.

 

class Employee {
    fullName!: string;
    age!: number;
    jobTitle!: string;

    details = ():void=>{
        console.log(`${this.fullName}의 나이는 ${this.age}이고, 직업은 ${this.jobTitle}입니다. `)
    }
}

 

프로퍼티에 !를 붙이게 되면 해당 프로퍼티의 값이 null이나 undifined가 아니라고 명시해주는 것입니다.

에러 메시지가 사라지고 간단하게 해결이 됐습니다.