basarat / basarat/typescript-book

Mapped types mistaken for index signature type

Open
#349 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
21.6k
Forks
2.6k
PR merge metrics
No merged PRs in 30d

Description

Hi @basarat!

I believe the chapter on index signature types may contain some slightly misleading information, although I could be wrong.

In the section title [Using a limited set of string literals](https://github.com/basarat/typescript-book/blob/master/docs/types/index-signatures.md#using-a-limited-set-of-string-literals), the example uses mapped types, which are a separate feature from index signature types.

As far as I understand, index signatures define types for all key lookups, whereas mapped types are a tool for programmatically creating interfaces with only known keys.

It took me awhile to understand the difference myself, especially as the syntax are so similar for the two:

``` ts
// Index signature
{ [index: string]: string }
// Mapped type
{ [key in 'foo']: string }
// … generates:
{ foo: string }
// The generated type has no index signature
```

Index signatures are useful for creating dictionaries aka maps, and mapped types are useful for creating records. https://gist.github.com/OliverJAsh/2a538639e35db183b0cc16ca8ab520a7

What do you think? My only concern is that mapped types are referred to as index signatures in this chapter, but as you can see from the example above, a mapped type does not generate a type with an index signature. They are different tools. 🤔

Contributor guide

Open the contributing guide

Research direction

Start with docs/types/index-signatures.md and review the “Using a limited set of string literals” section alongside the issue’s TypeScript examples. Clarify whether the example uses a mapped type rather than an index signature, and consider explaining the distinction; the documentation is done when the terminology and example accurately distinguish known keys from all key lookups.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.