You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
더 많은 예시를 보고 싶다면, [제안한 이슈](https://github.com/microsoft/TypeScript/issues/47920) 와 [이를 구현한 pull request](https://github.com/microsoft/TypeScript/pull/46827)를 확인하세요.
107
107
우리와 함께 이 기능을 구현한 [Oleksandr Tarasiuk](https://github.com/a-tarasyuk)에게 감사드립니다.
108
108
109
-
## Unlisted Property Narrowing with the `in` Operator
109
+
## "in" 연산자를 사용하여 정의되지 않은 프로퍼티로 타입 좁히기
110
110
111
-
As developers, we often need to deal with values that aren't fully known at runtime.
112
-
In fact, we often don't know if properties exist, whether we're getting a response from a server or reading a configuration file.
113
-
JavaScript's`in`operator can check whether a property
114
-
exists on an object.
111
+
개발자들은 자주 런타임에서 알 수 없는 값을 처리해야 할 때가 있습니다.
112
+
서버에서 응답받거나 설정 파일을 읽는 경우처럼 실제로 프로퍼티가 존재하는지 알 수 없는 경우가 흔하게 있습니다.
113
+
JavaScript의`in`연산자를 사용하면
114
+
객체에 프로퍼티가 존재하는지 알 수 있습니다.
115
115
116
-
Previously, TypeScript allowed us to narrow away any types that don't explicitly list a property.
116
+
이전 TypeScript 버전에서는 명시적으로 프로퍼티가 타입 목록에 없다면 범위를 좁힐 수 있었습니다.
117
117
118
118
```ts
119
119
interfaceRGB {
@@ -130,24 +130,24 @@ interface HSV {
130
130
131
131
function setColor(color:RGB|HSV) {
132
132
if ("hue"incolor) {
133
-
// 'color' now has the type HSV
133
+
//이제 'color'의 타입은 HSV 입니다.
134
134
}
135
135
// ...
136
136
}
137
137
```
138
138
139
-
Here, the type `RGB`didn't list the`hue` and got narrowed away, and leaving us with the type `HSV`.
139
+
여기서, `RGB`타입에 정의되지 않은`hue`에 의해 타입이 좁혀지게 되어, `HSV` 타입이 되었습니다.
140
140
141
-
But what about examples where no type listed a given property?
142
-
In those cases, the language didn't help us much.
143
-
Let's take the following example in JavaScript:
141
+
그러나 프로퍼티가 주어진 타입이 없는 경우에는 어떨까요?
142
+
그런 경우, 언어는 큰 도움이 되지 않습니다.
143
+
여기 JavaScript로 된 예시를 살펴보겠습니다.
144
144
145
145
```js
146
146
functiontryGetPackageName(context) {
147
147
constpackageJSON=context.packageJSON;
148
-
//Check to see if we have an object.
148
+
//객체 여부를 확인합니다.
149
149
if (packageJSON &&typeof packageJSON ==="object") {
150
-
//Check to see if it has a string name property.
150
+
//문자열 타입의 name 프로퍼티를 가지고 있는지 확인합니다.
151
151
if ("name"in packageJSON &&typeofpackageJSON.name==="string") {
152
152
returnpackageJSON.name;
153
153
}
@@ -157,8 +157,8 @@ function tryGetPackageName(context) {
157
157
}
158
158
```
159
159
160
-
Rewriting this to canonical TypeScript would just be a matter of defining and using a type for `context`;
161
-
however, picking a safe type like `unknown` for the `packageJSON` property would cause issues in older versions of TypeScript.
160
+
이것을 표준 TypeScript로 다시 작성한다면 `context` 타입을 정의해서 사용할 수 있습니다.
161
+
하지만 `packageJSON`의 프로퍼티에 `unknown`과 같은 안전한 타입을 사용하면 이전 TypeScript 버전에서 문제가 발생할 수 있습니다.
162
162
163
163
```ts
164
164
interfaceContext {
@@ -167,9 +167,9 @@ interface Context {
167
167
168
168
function tryGetPackageName(context:Context) {
169
169
const packageJSON =context.packageJSON;
170
-
//Check to see if we have an object.
170
+
//객체 여부를 확인합니다.
171
171
if (packageJSON&&typeofpackageJSON==="object") {
172
-
//Check to see if it has a string name property.
172
+
//문자열 타입의 name 프로퍼티를 가지고 있는지 확인합니다.
173
173
if ("name"inpackageJSON&&typeofpackageJSON.name==="string") {
174
174
// ~~~~
175
175
// error! Property 'name' does not exist on type 'object.
@@ -183,14 +183,14 @@ function tryGetPackageName(context: Context) {
183
183
}
184
184
```
185
185
186
-
This is because while the type of `packageJSON` was narrowed from `unknown` to `object`, the `in`operator strictly narrowed to types that actually defined the property being checked.
187
-
As a result, the type of `packageJSON` remained`object`.
TypeScript 4.9 also tightens up a few checks around how `in` is used, ensuring that the left side is assignable to the type `string | number | symbol`, and the right side is assignable to `object`.
216
-
This helps check that we're using valid property keys, and not accidentally checking primitives.
215
+
또한 TypeScript 4.9는`in`의 사용성에서 확인하는 부분을 강화하여 왼쪽에는 `string | number | symbol`, 오른쪽에는 `object`로만 할당할 수 있도록 보장합니다.
216
+
이를 이용해서 프로퍼티 키가 유효한지, 실수로 프리미티브 검증을 놓쳤는지 확인할 수 있습니다.
217
217
218
-
For more information, [read the implementing pull request](https://github.com/microsoft/TypeScript/pull/50666)
218
+
더 많은 정보를 얻고 싶다면, [이를 구현한 pull request를 읽어보세요](https://github.com/microsoft/TypeScript/pull/50666)
219
219
220
-
## <aname="auto-accessors-in-classes"> Auto-Accessors in Classes
220
+
## <aname="auto-accessors-in-classes"> 클래스의 자동 접근자
221
221
222
-
TypeScript 4.9 supports an upcoming feature in ECMAScript called auto-accessors.
223
-
Auto-accessors are declared just like properties on classes, except that they're declared with the `accessor` keyword.
222
+
TypeScript 4.9는 자동 접근자라고 하는 ECMAScript의 향후 기능을 지원합니다.
223
+
자동 접근자는 `accessor` 키워드로 선언된다는 점을 제외하면 클래스의 속성처럼 선언됩니다.
224
224
225
225
```ts
226
226
classPerson {
@@ -232,7 +232,7 @@ class Person {
232
232
}
233
233
```
234
234
235
-
Under the covers, these auto-accessors "de-sugar" to a `get`and`set`accessor with an unreachable private property.
235
+
내부적으로 이러한 자동 접근자는 도달할 수 없는 private 프로퍼티의 `get`및`set`접근자로 "de-sugar"됩니다.
236
236
237
237
```ts
238
238
classPerson {
@@ -251,7 +251,7 @@ class Person {
251
251
}
252
252
```
253
253
254
-
You can [read up more about the auto-accessors pull request on the original PR](https://github.com/microsoft/TypeScript/pull/49705).
254
+
[자세한 내용은 자동 접근자에 대한 원본 pull request](https://github.com/microsoft/TypeScript/pull/49705)에서 확인할 수 있습니다.
0 commit comments