microsoft / microsoft/TypeScript

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

Ouverte
#36,962 3 commentaires 28 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Awaiting More Feedback Suggestion
Langage dominant
Go
Étoiles
111k
Forks
14.4k
Merge moyen
1 j 19 h
PR mergées (30 j)
117

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.

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Commencez par localiser le diagnostic du vérificateur de types concernant une signature d’index de chaîne manquante, ainsi que les tests associés ou le point d’entrée de génération du diagnostic. Utilisez l’exemple Person et les exceptions listées pour vérifier quand la suggestion apparaît. Le travail est considéré comme terminé lorsque l’erreur ciblée inclut la suggestion de style keyof, sans modifier le comportement pour les objets vides, l’indexation numérique ou les clés hors périmètre.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
typescript
Domaine
compilers
Type d'issue
Fonctionnalité
Difficulté
4/5
Temps estimé
3-5 jours
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
35/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.