microsoft / microsoft/TypeScript
Support "evolving any" with forEach / internal functions
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Go
- Estrellas
- 111k
- Forks
- 14.3k
- Merge medio
- 2 d 4 h
- PR fusionados (30 d)
- 132
Descripción
TypeScript 2.1 introduced the notion of an "evolving" any type, a variable whose type is determined based on subsequent usage:
function square(xs: number[]) {
const ys = [];
for (const x of xs) {
ys.push(x * x);
}
return ys; // type is number[], hooray!
}
Unfortunately this is a bit brittle. If you ever introduce an internal function (say if you use forEach) then the inferred type is lost and you get a no implicit any error:
function square(xs: number[]) {
const ys = []; // Variable 'ys' implicitly has type 'any[]' in some locations
// where its type cannot be determined. (7034)
xs.forEach(x => {
ys.push(x * x);
});
return ys; // Variable 'ys' implicitly has an 'any[]' type. (7005)
}
The two forms do the same thing, so it's somewhat surprising that the for-of version passes the type checker but the forEach version does not.
I find the "evolving any" construct to be incredibly useful, but I have to scrap it every time I introduce an arrow function. I'm proposing that it be extended to work for cases like this.
TypeScript 3.7.2
Playground link
Expected behavior:
I'd expect both versions to pass the type checker and infer the type of ys as number[].
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 con el ejemplo vinculado de Playground y compara las formas for-of y forEach descritas en el issue. El cambio está completo cuando ambas formas pasan el comprobador de tipos e infieren ys como number[] sin errores de implicit-any.
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