File tree Expand file tree Collapse file tree
packages/playground-examples/copy/ko/3-7/Syntax and Messaging Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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/
You can’t perform that action at this time.
0 commit comments