타입스크립트에서 getter/setter
를 지원해준다.
그렇다면 왜 getter와 setter를 사용해서 데이터에 접근해야하는걸까 ?
Setter
: 예를 들어 Car 객체의 Speed 데이터는 0 이상이어야 한다라는 조건이 있는데, Speed를 외부에서 -1 로 변경 할 수 있다면 이는 객체의 무결성이 깨지는 것이다.Getter
: 외부에서 바로 데이터에 접근하지 않고 getter를 이용하여 객체에서 가공된 데이터를 반환해줄 수 있다.type Car = {
speed: number;
// ...
}
const value = 100;
const car: Car = {
speed: 0,
// ...
};
if (value < 0) {
throw new Error('Cannot set negative speed.');
}
account.speed = value;
class Car {
private carSpeed: number = 10;
get speed(): number {
return this.carSpeed;
}
set speed(value: number) {
if (value < 0) {
throw new Error('Cannot set negative speed.');
}
this.carSpeed = value;
}
// ...
}
const car = new Car();
account.car = 100;
setter
영역만 수정하면 된다. 타입스크립트에서는 public
private
protected
접근 제어자를 지원한다.
public
: 모든 class에서 접근 가능
private
: 자신이 포함된 class에서만 접근 가능
protected
: 상속받은 class와 자신이 속한 class에서만 접근 가능
그렇다면 왜 접근제어자를 사용해야할까?
class Circle {
radius: number;
constructor(radius: number) {
this.radius = radius;
}
perimeter() {
return 2 * Math.PI * this.radius;
}
surface() {
return Math.PI * this.radius * this.radius;
}
}
class Circle {
constructor(private readonly radius: number) {
}
perimeter() {
return 2 * Math.PI * this.radius;
}
surface() {
return Math.PI * this.radius * this.radius;
}
}
readonly
: 타입스크립트의 타입 시스템은 interface/class의 개별 속성을 readonly로 표시 할 수 있다. readonly로 표시된 속성은 변조가 불가능하고 참조만 가능하다.interface Config {
host: string;
port: string;
db : string;
}
readonly host:string;
readonly port:string;
readonly db:string;
ReadOnlyArray<T>
를 사용하여 읽기 전용 배열 생성 가능push()
fill()
과 같은 배열의 변경을 막는다. const arr = ReadonlyArray<number>=[1,3,5];
arr = [] // 에러
arr.push(100) // 에러
let, const
에 대한 타입 추론을 아래와 같이 제공한다. let hello = 'hello'; // 추론된 타입 'String'
const world = 'world'; // 추론된 타입 'world'
객체
는 어떻게 추론 될까? const obj = {
hello:'hello'
}
// const obj = { hello:string }
const
로 선언되었음에도 불구하고 객체 내부 프로퍼티에 대한 추론은 let
과 같이 진행된다. 당연히 객체 내부 프로퍼티는 수정가능하기 때문이다.Const Assertion
이다. Const Assertion
을 사용하면 let
이나 참조형
도 const 변수
처럼 타입 추론을 한다. const obj = {
hello:'hello'
} as const;
// const obj = { hello:'hello' }
obj.hello = 'world' // 에러
type EmailConfig = { // ... }
type DbConfig = { //... }
type Config = EmailConfig | DbConfig;
extends
혹은 implements
가 필요 할 때 interface Shape { //... }
interface CircleShape extends Shape { //... }
class Circle implements CircleShape { //... }