Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Document index types, indexed access types and mapped types #443

Merged
merged 9 commits into from
Dec 5, 2016

Conversation

sandersn
Copy link
Member

No description provided.

@sandersn
Copy link
Member Author

@DanielRosenwasser or @mhegazy mind taking a look?

@sandersn sandersn changed the title Document index and indexed access types Document index types, indexed access types and mapped types Nov 30, 2016
@@ -283,6 +283,197 @@ The right side of the `instanceof` needs to be a constructor function, and TypeS

in that order.

# Index types
Copy link
Contributor

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 Query
  • T[K] => Indexed Access Type
  • { [P in K] : T } => Mapped Types
    • { [P in keyof T]: F<T[K]>; } => Homomorphic Mapped Types

See microsoft/TypeScript#11943

Copy link
Member Author

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`.
Copy link
Contributor

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.

Copy link
Member Author

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.
Copy link
Contributor

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.

Copy link
Member Author

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] {
Copy link
Contributor

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]

Copy link
Member Author

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 &mdash; mapped types.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why &mdash; and not just -?

Copy link
Member Author

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';
Copy link
Contributor

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.

Copy link
Member Author

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;
}
```

Copy link
Contributor

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

Copy link
Member Author

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants