microsoft / microsoft/TypeScript
Object is possibly undefined after undefined check on readonly property
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 2g 4h
- PR unite (30g)
- 132
Descrizione
TypeScript Version: 3.7.4, 3.9.2
Search Terms:
Object is possibly undefined
undefined readonly callback
Code
class Test {
private readonly testProperty?: { value: number };
public constructor(testProperty: { value: number }) {
this.testProperty = testProperty;
}
public method() {
if (this.testProperty) {
Promise.resolve().then(() => this.testProperty.value); //Object is possibly undefined
}
}
}
Expected behavior:
testProperty should not be resolved as possibly undefined as this is not possible in this scenario as the property has already been checked and it is marked as readonly.
Actual behavior:
testProperty is possibly undefined, requiring another undefined check.
Related Issues:
Similar issues but these i understand why TypeScript would report an error, in my case the property is readonly so it seems like a bug to me.
https://github.com/microsoft/TypeScript/issues/38053
https://github.com/microsoft/TypeScript/issues/11498
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 dalla riproduzione collegata in Playground e confronta il comportamento descritto nelle issue correlate #38053 e #11498. Traccia il controllo dei tipi della readonly optional property all’interno del callback di Promise; il lavoro è completato quando l’esempio fornito non segnala più «possibly undefined» oppure il comportamento previsto viene risolto in altro modo.
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
- 35/100