microsoft / microsoft/TypeScript

Unexpected circularity error when variable is initialized from a class member

Aperta
#61,606 4 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Bug Domain: check: Type Circularity Help Wanted
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

### 🔎 Search Terms

loss of type, for loop, for-loop, class field

### 🕗 Version & Regression Information

- This changed between versions ______ and _______
- This changed in commit or PR _______
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________
- I was unable to test this on prior versions because _______

### ⏯ Playground Link

https://www.typescriptlang.org/play/?#code/JYOwLgpgTgZghgYwgAgIIAd0BtgLmYAexGQG8AoZK5YAEwC5kBnMKUAcwG5zLq9J2hKAE9GFahJoNmrDtwkBfckvIIscJkzSYc-IiC3jq6AK4AjXcgBioWhmy58+gBQBHE9FEy2IdgEpGe10nYmQAH2QQEywsMl5JZCgIMBMoEiiY+WolFTUNLQAVCBY4iQBlCCwIBEg7HUcCYiZGCrAAHhYfdgA+ZABeSIgAd2RW5z8sqiCG-WbtBz0m8Mjo2IGMrEnkdmTRyura6cWDACFhAGF8CEERccYABTgoAjgsNoAlaqFaDtlfABo9u1OhxumDlhtSglkAB6GHIADqQgA1k9CCYQLRkABGRgAW0IADcUGAABbALQWQgIZE0Ax0FAwITILCEQjoeKSOGIlFojFYgBMLTgxOQAAMyRSAHRHEIGMXIMCEZBwZAIJpgRWkiAkExMEmk-CciTAGDIZySpgy+rHLR9e0rGJ+KHQ6hJFJpR2bY3ZHiu9UGTVJJjRMAPJ4vN6fdVQH4ggFA35dMG9AakBTcH1UJlQc0BkpwG1ygCSABFkIQzZapRUqjUIHUFnKmM6jK6qPnNYWm40SANq7Le1abJjBy5u8Fe2WJlnJKbzROZqF7etVq3Z-7iAQohAtq6VO2+Bq1VcbsIy-0VUXe1L+NchMIpXQ99DO1ee7NL8HQwBtO9nssAF1kAAfhAwYRlaJNQXGF8EkXW0pTgWhaGcBC5SfWgZ0PKhuXOQg8TxHUwBhDF1UI4itUZQgYkIIYOBZUASWVfUUGATUzCSOBkVIkBOIgbiNzdYpf3-B8gMvdChzg5AD2hd1UhIb8sDALYciAA

### 💻 Code

```ts
interface Application {
id: string;

category: {
id: string;
}
}

class Applications {
public FindApplication(query: string): Application | null {
return null;
}
}

class Test {
SelectedApplications: Set = new Set();
Applications: Applications | null = null;
get SelectedApplicationsByCategory(): Partial>> | null {
// Workaround 1: move this block inside for loop
// Workaround 2: Save this.Applications to a const then use that
if (this.Applications === null) {
return null;
}

const result: Partial>> = {};

for (const applicationID of this.SelectedApplications) {
const application = this.Applications.FindApplication(applicationID);
if (application === null) {
continue;
}

const categoryID = application.category.id;
const applications = result[categoryID] ?? new Set();
applications.add(application.id);
// Comment/uncomment the following line to see it break/unbreak
result[categoryID] = applications;
}

return result;
}
}
```

### 🙁 Actual behavior

`application` has type: `any`. `FindApplication` returns `Application | null`, but that gets lost somewhere.

### 🙂 Expected behavior

type for `application` should be `Application | null`.

### Additional information about the issue

See comments in code for workarounds and to see another line break type inference.

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 la riproduzione collegata in TypeScript Playground e confronta i due workaround documentati nel getter. Traccia il motivo per cui l'assegnazione a result[categoryID] modifica il tipo inferito di application; il lavoro è completato quando application rimane Application | null senza nessuno dei due workaround.

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
Specificata chiaramente
Idoneità per principianti
42/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.