14장 구문확장
- 열거형 : 키와 값의 일반 객체와 유사한 간단한 구문
- 네임스페이스 : 코드를 구조화하고 배열하는 최신 모듈보다 앞선 해결책
14.1 클래스 매개변수 속성
- 자바스크립트 클래스에서는 생성자에서 매개변수를 받고 즉시 클래스 속성에 할당하는 것이 일반적입니다.
- 타입스크립트는 이러한 종류의 매개변수 속성을 선언하기 위한 단축 구문을 제공합니다.
class Engineer {
readonly area: string;
constructor(area: string) {
this.area = area;
console.log("I'm working in the ${area} area.");
}
}
타입: string
new Engineer('mechanical').area;
- 속성은 클래스 생성자의 시작 부분에 동일한 타입의 맴버 속성으로 할당됩니다.
- 생성자의 매개변수 앞에 readonly 또는 public, protected, private 제한자 중 하나를 배치하면 타입스크립트가 동일한 이름과 타입의 속성도 선언하도록 지시합니다.
class Engineer {
constructor(readonly area: string);
console.log("I'm working in the ${area} area.");
}
}
타입: string
new Engineer('mechanical').area;
- 매개변수의 속성은 클래스 생성자의 맨 처음에 할당됩니다.
14.2 데코레이터
14.3 열거형
enum Wat {
FirstString: 'first',
SomeNumber= 9000,
ImplicitNumber,
AnotherString = 'another',
NotAllowed,
}
14.4 네임스페이스
14.4.4 네임스페이스보다 모듈을 선호함