아이템24
일관성 있는 별칭 사용하기
- 별칭을 남발하면 제어 흐름을 분석하기 어렵다.
- 별칭은 타입 스크립트의 타입 좁히기를 어렵게 만든다.
function isPointInPolygon(polygon: Polygon, pt: Coordinate) {
const box = polygon.bbox; // 타입이 BoundingBox | undefined
if(polygon.bbox){
polygon.bbox // 타입이 BoundingBOx
box // 타입이 BoundingBox | undefined
}
}
- 비구조화 문법(동일한 키 값 사용하여 자동으로 할당)을 사용하여 일관된 이름 사용하는 것이 좋음
interface Coordinate{
x:number;
y:number;
}
interface BoundingBox{
x: [number, number];
y: [number, number];
}
interface Polygon{
bbox?: BoundingBox;
}
function isPointInPolygon(polygon: Polygon, pt:Coordinate){
const {bbox} = polygon; // 객체의 속성명과 동일한 변수명 사용하여 비구조화 문법 적용
if(bbox){
const {x,y} = bbox; // 객체의 속성명과 동일한 변수명 사용하여 비구조화 문법 적용
if(pt.x < x[0] || pt.x > x[1] ||
pt.y < y[0] || pt.y > y[1]}{
return false;
}
}
}