We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 5d08ebf commit e485ef7Copy full SHA for e485ef7
README.md
@@ -56,4 +56,4 @@
56
- [x] 아이템 34. 부정확한 타입보다는 미완성 타입을 사용하기
57
- [x] 아이템 35. 데이터가 아닌, API와 명세를 보고 타입 만들기
58
- [x] 아이템 36. 해당 분야의 용어로 타입 이름 짓기
59
-- [ ] 아이템 37. 공식 명칭에는 상표를 붙이기
+- [x] 아이템 37. 공식 명칭에는 상표를 붙이기
ch04_타입_설계/item37.md
@@ -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