microsoft / microsoft/TypeScript
Support Implicit Generics Inference
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Go
- Estrellas
- 111k
- Forks
- 14.4k
- Merge medio
- 1 d 19 h
- PR fusionados (30 d)
- 117
Descripción
Search Terms
implicit generics, broader type inference, better type inference
Suggestion
I'd like TypeScript to know when two types are unknown, but equal.
That is, say I've function equals():
type T = { type: 'a', p1: boolean } | { type: 'b', p2: string };
function equals(a: T, b: T) {
// Currently, we have to write:
if (a.type === 'a' && b.type === 'a') { return a.p1 === b.p1; }
if (a.type === 'b' && b.type === 'b') { return a.p2 === b.p2; }
return false;
// But I'd like to be able to write:
if (a.type !== b.type) { return false; }
return a.type === 'a' ? a.p1 === b.p1 : a.p2 === b.p2;
}
Use Cases
I can't think about other use cases except comparisons (if you know let me know), but I think they're common enough. This will also make them somewhat faster (no need to check both's type).
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
No se nombran archivos del repositorio, pruebas ni puntos de entrada. Empieza reproduciendo el ejemplo de equals en TypeScript y determina el comportamiento de narrowing esperado cuando los discriminantes son iguales. Se considera terminado cuando la inferencia implícita solicitada funciona sin romper el comportamiento existente de type-checking, con pruebas que cubran el ejemplo.
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
- 5/5
- Tiempo estimado
- Más de una semana
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 35/100