Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Object Types.md #3081

Open
wants to merge 1 commit into
base: v2
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions packages/documentation/copy/en/handbook-v2/Object Types.md
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,23 @@ interface NotOkay {

</details>

You can also use the `in` keyword with a union of literal types to denote a set of properties with a common type:

```ts twoslash
type FeatureOptions = {
[key in 'darkMode' | 'newUserProfile']: bool
}
```

The above example is equivalent to:

```ts twoslash
type FeatureOptions = {
darkMode: bool;
newUserProfile: bool;
}
```

While string index signatures are a powerful way to describe the "dictionary" pattern, they also enforce that all properties match their return type.
This is because a string index declares that `obj.property` is also available as `obj["property"]`.
In the following example, `name`'s type does not match the string index's type, and the type checker gives an error:
Expand Down