microsoft / microsoft/TypeScript

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

Offen
#36,962 3 Kommentare 28 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Awaiting More Feedback Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.4k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne damit, die Type-Checker-Diagnose für eine fehlende String-Index-Signatur sowie die zugehörigen Tests oder den Einstiegspunkt für die Diagnosegenerierung zu finden. Verwende das Person-Beispiel und die aufgeführten Ausnahmen, um zu überprüfen, wann der Vorschlag erscheint. Als erledigt gilt die Aufgabe, wenn der gezielte Fehler den Vorschlag im keyof-Stil enthält, ohne das Verhalten für leere Objekte, die Indizierung mit Zahlen oder Schlüssel außerhalb des Gültigkeitsbereichs zu ändern.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Feature
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.