microsoft / microsoft/TypeScript
Support "evolving any" with forEach / internal functions
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.3k
- Ø Merge
- 2 T. 4 Std.
- Gemergte PRs (30 T.)
- 132
Beschreibung
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[].
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne mit dem verknüpften Playground-Beispiel und vergleiche die im Issue beschriebenen for-of- und forEach-Formen. Die Änderung ist abgeschlossen, wenn beide Formen den Typprüfer bestehen und ys als number[] ohne implicit-any-Fehler inferieren.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Feature
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100