Repository navigation
Type narrowing for awaited values #30551
Copy link
Copy link
Open
Labels
Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScriptAn idea for TypeScript
Description
Activity
MartinJohns commented
on Mar 23, 2019 ContributorMore actionsThis is due to #9998.
DanielRosenwasser commented
on Mar 25, 2019 MemberMore actionsI 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.
- addedAwaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScriptAn idea for TypeScript
on Mar 26, 2019 I guess this is the same issue that
for-await-ofdoesn't narrow types?/// <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) } }
Metadata
Metadata
Assignees
Labels
Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScriptAn idea for TypeScript
Search Terms
promise, type narrowing, await
Suggestion
The type narrowing that occurs inside
ifstatements, should work with awaited valuesThough generally speaking a type is used for values that are already
Examples
Checklist
My suggestion meets these guidelines: