microsoft / microsoft/TypeScript
When an index signature is not available, encourage using a more specific type to index the type.
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 2g 4h
- PR unite (30g)
- 132
Descrizione
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.keysandObject.entriesas 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.
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia individuando la diagnostica del type-checker per una firma di indice string mancante e i test correlati o il punto di ingresso per la generazione della diagnostica. Usa l’esempio Person e le eccezioni elencate per verificare quando viene visualizzato il suggerimento. Il lavoro è completato quando l’errore specifico include il suggerimento in stile keyof senza modificare il comportamento per gli oggetti vuoti, l’indicizzazione numerica o le chiavi fuori ambito.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- typescript
- Ambito
- compilers
- Tipo di issue
- Funzionalità
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100