Suggest specifying generic as union if candidates are different
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 5/5
- Tiempo estimado
- Más de una semana
- Aptitud para principiantes
- 35/100
- Tipo de issue
- Nueva funcionalidad
- Claridad
- Bastante claro
- Estado de actividad
- Estancado
- Stack tecnológico
- typescript
- Área
- compilers
Línea de trabajo
Usa los ejemplos de compare y match del issue como reproducciones y comprueba primero sus diagnostics con la versión actual de TypeScript. Se considera terminado cuando el diagnostic explica los distintos candidatos de inferencia y deja clara la corrección mediante un tipo unión explícito cuando esa es la solución prevista.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
TypeScript Version: 2.6.1.
Code
If a generic type can't be formed by picking one of the inference candidates, you'll get the error you posted.
This makes sense, however I would like to question whether we can improve the user experience around this, so errors due to this constraint are easier to understand and fix.
For example:
{
function compare<T>(x: T, y: T): number {
return 1;
}
compare(
'oops',
/* Argument of type '42' is not assignable to parameter of type 'string'. */
42,
);
}
{
function match<T>(cases: { foo: T; bar: T }): T {
return cases.foo;
}
/*
Argument of type '{ foo: number; bar: string; }' is not assignable to parameter of type '{ foo: number; bar: number; }'.
Types of property 'bar' are incompatible.
Type 'string' is not assignable to type 'number'.
*/
match({
foo: 1,
bar: 'foo',
});
}
As a TypeScript user, I have struggled with these errors many times, and I've only recently realised the specific constraint on the type system which is the root cause of these errors: generics are picked from the first candidate and are not widened to include all candidates. I have also seen other people struggle with this when learning TypeScript.
We can fix this error by specifying the generic as a union:
match<string | number>({
foo: 1,
bar: 'foo',
});
However, this fix is really not obvious from the error message, especially if the user is not aware of this constraint on the type system (that generics will not be inferred as unions).
I'm wondering if there's any way we can better surface this constraint to the user, to make it clearer to users how they can fix these type errors, such as by specifying the generic as a union (if that is what they intend).
- Lenguaje dominante
- Go
- Estrellas
- 111k
- Forks
- 14.4k
- Merge medio
- 1 d 19 h
- PR fusionados (30 d)
- 117
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.
Más de microsoft/TypeScript
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 88/100
microsoft/TypeScript#64322 · 2 comentarios · 1 reacción · 2 asignados ·
-
Possible Improvement
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
microsoft/TypeScript#64278 · 1 comentario · 1 reacción ·
-
Docs
Dificultad 2/5 1-3 horas Aptitud para principiantes 70/100
microsoft/TypeScript#64118 · 1 comentario ·
-
Dificultad 1/5 Menos de una hora Aptitud para principiantes 88/100
microsoft/TypeScript#64094 ·
-
Docs
Dificultad 2/5 1-3 horas Aptitud para principiantes 76/100
microsoft/TypeScript#63959 · 5 comentarios ·
Todos los issues de microsoft/TypeScript
Issues similares
-
optimization optimization:agents-md-curator
Dificultad 2/5 1-3 horas Aptitud para principiantes 86/100
githubnext/gh-aw-cao#13143 ·
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 84/100
blinklabs-io/bursa#904 ·
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 84/100
yanet-platform/ipfw-go#129 ·
-
bug confmap/provider/googlesecretmanagerprovider needs triage
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
open-telemetry/opentelemetry-collector-contrib#51273 · 2 comentarios ·
-
bug: AI Gateway client filter lists "Unknown" twice when NULL and literal Unknown clients coexist Abiertobug
Dificultad 2/5 1-3 horas Aptitud para principiantes 90/100