Document index types, indexed access types and mapped types#443
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.
pages/Advanced Types.md
Outdated
|
|
||
| in that order. | ||
|
|
||
| # Index types |
There was a problem hiding this comment.
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.
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.
pages/Advanced Types.md
Outdated
| 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.
is the union of known public property names of T.
pages/Advanced Types.md
Outdated
| 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.
not sure this line adds much value.
pages/Advanced Types.md
Outdated
| 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.
typo: return type should be T[K]
pages/Advanced Types.md
Outdated
| } | ||
| ``` | ||
|
|
||
| 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.
why — and not just -?
There was a problem hiding this comment.
- isn't long enough and -- doesn't look good when rendered to HTML.
pages/Advanced Types.md
Outdated
| Let's take a look at the simplest mapped type and its parts: | ||
|
|
||
| ```ts | ||
| type Properties = 'option1' | 'option2'; |
pages/Advanced Types.md
Outdated
| [P in K]: T; | ||
| } | ||
| ``` | ||
|
|
There was a problem hiding this comment.
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.
good idea. I will add that shortly.
The feature depends on type aliases and string literal types, so should come after those.
No description provided.