microsoft / microsoft/TypeScript
Object is possibly undefined after undefined check on readonly property
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the linked Playground reproduction and compare the behavior described in related issues #38053 and #11498. Trace type-checking for the readonly optional property inside the Promise callback; done means the supplied example no longer reports “possibly undefined” or the expected behavior is otherwise resolved.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100