-
Notifications
You must be signed in to change notification settings - Fork 12.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Type narrowing for awaited values #30551
Labels
Awaiting More Feedback
This means we'd like to hear from more people who would be helped by this feature
Suggestion
An idea for TypeScript
Comments
This is due to #9998. |
I think it's more that there's a limited set of syntactic constructs we can analyze because it would be inefficient otherwise. I guess the thing I want to know is whether there's a reason to write code like that. |
RyanCavanaugh
added
Awaiting More Feedback
This means we'd like to hear from more people who would be helped by this feature
Suggestion
An idea for TypeScript
labels
Mar 26, 2019
I guess this is the same issue that /// <reference lib="esnext.asynciterable"/>
interface MyIterator<T> {
next(): Promise<
| {
done: false;
value: T;
}
| {
done: true;
value: undefined;
}
>;
[Symbol.asyncIterator](): MyIterator<T>;
}
interface Item { name: string }
async function m(iter: MyIterator<Item>) {
for await (const it of iter) {
console.log(it.name); // Error here complaining about 'it': Object is possibly 'undefined'.ts(2532)
}
} |
hmm, actually it is not related to await. iterator has the same error interface MyIterator<T> {
next():
| {
done: false;
value: T;
}
| {
done: true;
value: undefined;
}
;
[Symbol.iterator](): MyIterator<T>;
}
interface Item { name: string }
function m(iter: MyIterator<Item>) {
for (const it of iter) {
console.log(it.name); // Error here complaining about 'it': Object is possibly 'undefined'.ts(2532)
}
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
Awaiting More Feedback
This means we'd like to hear from more people who would be helped by this feature
Suggestion
An idea for TypeScript
Search Terms
promise, type narrowing, await
Suggestion
The type narrowing that occurs inside
if
statements, should work with awaited valuesThough generally speaking a type is used for values that are already
Examples
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: