Skip to content

Commit e485ef7

Browse files
committed
아이템37 정리
1 parent 5d08ebf commit e485ef7

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,4 @@
5656
- [x] 아이템 34. 부정확한 타입보다는 미완성 타입을 사용하기
5757
- [x] 아이템 35. 데이터가 아닌, API와 명세를 보고 타입 만들기
5858
- [x] 아이템 36. 해당 분야의 용어로 타입 이름 짓기
59-
- [ ] 아이템 37. 공식 명칭에는 상표를 붙이기
59+
- [x] 아이템 37. 공식 명칭에는 상표를 붙이기

ch04_타입_설계/item37.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# 공식 명칭에는 상표를 붙이기
2+
3+
타입스크립트는 구조적 타이필(덕 타이핑)을 사용하기 때문에, 값을 세밀하게 구분하지 못하는 경우가 있다. 값을 구분하기 위해 공식 명칭이 필요하다면 상표를 붙이는 것을 고려해야 한다.
4+
5+
```ts
6+
type Meters = number & { _brand: "meters" };
7+
type Seconds = number & { _brand: "seconds" };
8+
9+
const meters = (m: number) => m as Meters;
10+
const seconds = (s: number) => s as Seconds;
11+
12+
const oneKm = meters(1000); // 타입이 Meters
13+
const oneMin = seconds(60); // 타입이 Seconds
14+
```

0 commit comments

Comments
 (0)