-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Closed as not planned
Labels
DuplicateAn existing issue was already createdAn existing issue was already created
Description
π Search Terms
"undefined check async"
π Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about "undefined", "async"
β― Playground Link
π» Code
type Foo = {
value: string,
}
class Test {
private foo: undefined | Foo = undefined;
public async doSomething() {
this.foo = {
value: 'a',
};
try {
await new Promise((r) => setTimeout(r, 500));
} finally {
console.log('foo', this.foo.value);
this.foo = undefined;
}
}
public unset() {
this.foo = undefined;
}
}
(async () => {
const t = new Test();
const p = t.doSomething();
t.unset()
await p;
})()π Actual behavior
Typescript compiles without errors.
Running the code throws TypeError: Cannot read properties of undefined (reading 'value')
π Expected behavior
Typescript should throw an error that this.foo might be undefined.
Additional information about the issue
Typescript successfully detects that this.foo can be undefined before the assignment in the function.
But it seems to not take into account that the value might change when executed async.
gnarlex, nathanael-ruf, romfrolov, NJona, DanielCodesphere and 5 more
Metadata
Metadata
Assignees
Labels
DuplicateAn existing issue was already createdAn existing issue was already created