microsoft / microsoft/TypeScript

TypeScript can't infer correct type from while loop if while condition is given a promise

Aperta
#55,300 5 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Domain: check: Control Flow Help Wanted Possible Improvement
Lingua principale
Go
Stelle
111k
Fork
14.4k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

🔎 Search Terms

promise in while loop condition

🕗 Version & Regression Information
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about Bugs that aren't bugs
⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.2.0-dev.20230807#code/GYVwdgxgLglg9mABAZwDYFN0AcAUBbZALkTBDwCN0AnASmIAUq48Zl0AeKKkdAPkQDeAKESIq6KCCpIw6AO6JGzVhy49eOAIaIAvPzZQAKjDzo4IKDhw1d-TTjXoaAGkQEaNIQF8hQzcgBPSERQSFgERHQAD008LAwARmsGJhY2dmQuGDAAc35hAEgMKEQAN01UHmJMqmycxAAfRHAAE3RgbPQWoQK5AAsYDEQHbidBHoLNOU0YErRMXABWAAZVzwKC8sr0XUQAckW9ifJxTQBrHp8C8UlpMoqeb19-IIgQ8Gh4JGjY+PQAJmSilSKgyWVy+R6xXu22q4PqTVa7U63V6AyGWmmsxQGGwOBWaxshU2Dx2On2h2OpwuBSuNykSC2jy8QA

💻 Code
function sleep(ms: number): Promise<true> {
  return new Promise<true>(a => setTimeout(() => a(true), ms))
}

async function example1(): Promise<string> {
	let value: string | undefined
	while (true) {
		await sleep(5000)
		value = '5'
		break
	}
	return value
}

async function example2(): Promise<string> {
	let value: string | undefined
	while (await sleep(5000)) {
		value = '5'
		break
	}
	return value
}
🙁 Actual behavior

In example2, TypeScript can't infer the type of value and that it should be now a string and not undefined simply because the Promise that returns true in the while loop has been moved to the while condition instead of the body of the while loop.

🙂 Expected behavior

The expected behaviour should be that TypeScript should be able to infer that it is still a string at the return statement just like in example1

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia con il TypeScript Playground collegato e confronta l’analisi del flusso di controllo per example1 ed example2, concentrandoti sulla Promise attesa nella condizione while. Il lavoro è completato quando il secondo esempio viene accettato come restituzione di Promise senza indebolire i tipi riportati né modificare il comportamento previsto dell’esempio.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
38/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.