microsoft / microsoft/TypeScript

When an index signature is not available, encourage using a more specific type to index the type.

Open
#36,962 3 comments 28 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

Search Terms

index signature, keyof, domain: error messages
Vaguely related:
https://github.com/Microsoft/TypeScript/issues/14951

Suggestion

When there is a type error because of a missing string index signature, add a suggestion of:
"Did you mean to use a more specific type such as keyof Thing instead of string?"

Only show this message:

  • if the type used to index is within the same scope
  • the object being indexed is not empty

Use Cases

Take the following example:

interface Person {
  name: string;
  phone: string;
}
declare const person: Person;

const get = (person: Person, key: string) => {
  return person[key];
};

get(person, "name");

The error message here is:

Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'Person'.
  No index signature with a parameter of type 'string' was found on type 'Person'.

This is technically correct! However, many people read this error message to mean "add an index signature to Person," rather than advising them to use a more specific type than string, like keyof Person. Adding the index signature suppresses the error, making it seem like the correct solution.

Where this doesn't work

There are some cases where this advice would not make sense:

// Did you mean to use a more specific type such as `keyof Number` instead of `string`?
1["a" as string]

Other cases I can think of so far:

  • Empty objects, especially from reduce (suppress for empty objects)
  • Indexing an object by number (only do this for strings?)
  • Object.keys and Object.entries as discussed many, many times (require the indexing type to be in the same scope)

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the type-checker diagnostic for a missing string index signature and the related tests or diagnostic-generation entry point. Use the Person example and the listed exceptions to verify when the suggestion appears. Done means the targeted error includes the keyof-style suggestion without changing behavior for empty objects, number indexing, or out-of-scope keys.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.