티스토리 뷰
Typescript
[typescript] class 에서 Property has no initializer and is not definitely assigned in the constructor 오류 해결 방법
hiolivia 2021. 9. 3. 11:52class 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가 아니라고 명시해주는 것입니다.
에러 메시지가 사라지고 간단하게 해결이 됐습니다.
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- input placeholder
- 자바스크립트
- curl.cainfo
- php mysql 랜덤 숫자
- curl.cainfo 에러
- react restful API
- php composer 설치
- 링크 공유 시 썸네일
- JS 시계 기능
- http 접속 시 https 리다이렉션
- php mongodb
- php 게시판
- curl ssl 인증서 다운로드
- 자바스크립트 url 파라미터 추출
- php composer
- JS session
- fetch post 데이터 전송
- fetch get post 전송
- HTML
- 링크 공유 시 정보 수정
- curl ssl 인증서
- react fetch
- AJAX
- 카톡 링크 공유 썸네일
- 자바스크립트 세션
- JS url 파라미터
- curl.cainfo 적용
- JavaScript
- php rand()
- html input
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
글 보관함