-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Document index types, indexed access types and mapped types #443
Conversation
@DanielRosenwasser or @mhegazy mind taking a look? |
Based on the more involved discussion from the blog post.
@@ -283,6 +283,197 @@ The right side of the `instanceof` needs to be a constructor function, and TypeS | |||
|
|||
in that order. | |||
|
|||
# Index types |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure where is a good place to surface this terminology:
keyof T
=> Index Type QueryT[K]
=> Indexed Access Type{ [P in K] : T }
=> Mapped Types{ [P in keyof T]: F<T[K]>; }
=> Homomorphic Mapped Types
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mention the names below in the text (except homomorphic mapped types) but they're kind of buried. I'll think about ways to call them out.
The compiler checks that `name` is actually a property on `Person`, and it knows that `strings` is a `string[]` because `name` is a `string`. | ||
To make this work, the example introduces a couple of new type operators. | ||
First is `keyof T`, the index type query operator. | ||
For any type `T`, `keyof T` is the union of property names of `T`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is the union of known public property names of T
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
To make this work, the example introduces a couple of new type operators. | ||
First is `keyof T`, the index type query operator. | ||
For any type `T`, `keyof T` is the union of property names of `T`. | ||
The syntax is similar to `typeof` except that it works on types instead of values. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure this line adds much value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
true
Here's another example with a function named `getProperty`. | ||
|
||
```ts | ||
function getProperty<T, K extends keyof T>(o: T, name: K): K[T] { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo: return type should be T[K]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed.
} | ||
``` | ||
|
||
This happens often enough in Javascript that TypeScript provides a way to create new types based on old types — mapped types. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why —
and not just -
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
-
isn't long enough and --
doesn't look good when rendered to HTML.
Let's take a look at the simplest mapped type and its parts: | ||
|
||
```ts | ||
type Properties = 'option1' | 'option2'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i would call this Keys
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure
[P in K]: T; | ||
} | ||
``` | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should add some notes about inferring from mapped types. a common question is how to get the "un-proxified" or "un-partial" version of a value. see microsoft/TypeScript#12578 (comment) for an example.
Also worth noting how inference works from homomorphoic types, see microsoft/TypeScript#12528
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good idea. I will add that shortly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
The feature depends on type aliases and string literal types, so should come after those.
No description provided.