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
{{ message }}
This repository was archived by the owner on Oct 12, 2022. It is now read-only.
Copy file name to clipboardExpand all lines: pages/release notes/TypeScript 2.1.md
+26-38Lines changed: 26 additions & 38 deletions
Original file line number
Diff line number
Diff line change
@@ -1,9 +1,12 @@
1
1
# `keyof` and Lookup Types
2
2
3
-
Many libraries take advantage of the fact that objects are (for the most part) just a map of strings to values.
4
-
Given what TypeScript knows about each value's properties, there's a set of known strings (or keys) that you can use for lookups.
3
+
In JavaScript it is fairly common to have APIs that expect property names as parameters, but so far it hasn't been possible to express the type relationships that occur in those APIs.
5
4
6
-
That's where the `keyof` operator comes in.
5
+
Enter Index Type Query or `keysof`;
6
+
An indexed type query `keyof T` yields the type of permitted property names for `T`.
7
+
A `keyof T` type is considered a subtype of `string`.
8
+
9
+
##### Example
7
10
8
11
```ts
9
12
interfacePerson {
@@ -12,58 +15,43 @@ interface Person {
12
15
location:string;
13
16
}
14
17
15
-
let propName:keyofPerson;
16
-
```
17
-
18
-
`keyof Person` is equivalent to having written out
0 commit comments