Skip to content

Commit 4e36e2d

Browse files
committed
Translate Nullish Coalescing to ko
1 parent f976e05 commit 4e36e2d

1 file changed

Lines changed: 43 additions & 0 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//// { compiler: { }, order: 2 }
2+
3+
// nullish coalescing 연산자는 || 연산자의 대안입니다.
4+
// 왼쪽의 결과가 null 또는 undefined일 경우,
5+
// 오른쪽의 결과를 반환합니다.
6+
7+
// 그에 반해, ||는 falsy 검사를 사용하므로
8+
// 빈 문자열 또는 숫자 0은 false로 여깁니다.
9+
10+
// 이 기능의 좋은 예시는 key가 전달되지 않았을 때
11+
// 기본값을 갖는 일부분의 오브젝트를 다루는 것입니다.
12+
13+
interface AppConfiguration {
14+
// 기본값: "(no name)"; 빈 문자열은 유효
15+
name: string;
16+
17+
// 기본값: -1; 0은 유효
18+
items: number;
19+
20+
// 기본값: true
21+
active: boolean;
22+
}
23+
24+
function updateApp(config: Partial<AppConfiguration>) {
25+
// null-coalescing 연산자로 설정
26+
config.name = config.name ?? "(no name)";
27+
config.items = config.items ?? -1;
28+
config.active = config.active ?? true;
29+
30+
// 현재 솔루션
31+
config.name = typeof config.name === "string" ? config.name : "(no name)";
32+
config.items = typeof config.items === "number" ? config.items : -1;
33+
config.active = typeof config.active === "boolean" ? config.active : true;
34+
35+
// 잘못된 데이터를 설정할 수 있는 || 연산자 사용
36+
config.name = config.name || "(no name)"; // "" 입력을 허용하지 않음
37+
config.items = config.items || -1; // 0 입력을 허용하지 않음
38+
config.active = config.active || true; // 아주 잘못된 사례, 항상 true
39+
}
40+
41+
// 여러분은 3.7 버전에 대한 블로그 글에서 nullish coalescing에 대해 더 많은 것을 읽어보실 수 있습니다:
42+
//
43+
// https://devblogs.microsoft.com/typescript/announcing-typescript-3-7/

0 commit comments

Comments
 (0)