코드/Trouble Shoot

[TS] has no initializer and is not definitely assigned in the constructor

Yeah-Panda 2020. 2. 28. 19:14

이런 코드가 있다고 가정하자.

class ClassA {

    private container: Container;
    private config: Config

    public constructor() {
        this.container = new Container();
    }

    public setConfig(config: Config) {
        this.config = config;
    }
}

//Property 'config' has no initializer and is not definitely assigned in the constructor.ts(2564) 가 발생한다.
-

아마 타입스크립트 2.7.1 버전부터
추가되었던 --strictPropertyInitialization 옵션으로 인해 생기는 린트 오류다. [참조]
해결책은 간단

// 인라인 해결책으로는
private config!: Config

// 보다 근본적으로 설정을 바꿀 수 있다.
// tsconfig.json
"strictPropertyInitialization": false, // 얘만 추가해주면 된다.

'코드 > Trouble Shoot' 카테고리의 다른 글

[Git] 브랜치 이름 변경하기  (0) 2020.03.24
[JS] input 숫자만 입력하기  (0) 2020.03.19
[VS Code] 파일 뷰의 indent  (0) 2020.02.28
노트북화면 반전  (0) 2015.12.24
[엑셀] 방향키 이동이 안될때  (0) 2015.08.10