microsoft / microsoft/TypeScript
When an index signature is not available, encourage using a more specific type to index the type.
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Go
- Estrellas
- 111k
- Forks
- 14.3k
- Merge medio
- 1 d 19 h
- PR fusionados (30 d)
- 117
Descripción
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.
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Comienza localizando el diagnóstico del comprobador de tipos para una firma de índice de string ausente y las pruebas relacionadas o el punto de entrada de la generación del diagnóstico. Usa el ejemplo Person y las excepciones enumeradas para verificar cuándo aparece la sugerencia. Se considera terminado cuando el error específico incluye la sugerencia de estilo keyof sin cambiar el comportamiento para objetos vacíos, la indexación numérica ni las claves fuera del ámbito.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- typescript
- Área
- compilers
- Tipo de issue
- Nueva funcionalidad
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 35/100