microsoft / microsoft/TypeScript
Support "evolving any" with forEach / internal functions
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 2g 4h
- PR unite (30g)
- 132
Descrizione
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[].
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia con l’esempio collegato di Playground e confronta le forme for-of e forEach descritte nell’issue. La modifica è completa quando entrambe le forme superano il type checker e inferiscono ys come number[] senza errori di implicit-any.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- typescript
- Ambito
- compilers
- Tipo di issue
- Funzionalità
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100