Skip to content

Consider all constituents of contextual return type when resolving type of 'yield' #56222

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

Closed
wants to merge 3 commits into from

Conversation

rbuckton
Copy link
Member

@rbuckton rbuckton commented Oct 25, 2023

This avoids an early-exit in getIterationTypesOfIterable when the intended use is resolving the iteration types of a contextual return type of a generator function:

let f: () => Generator<string, void, string> | AsyncGenerator<string | void | string>;
f = function*() {
  let name: string;
  name = yield ""; 
};

The above code is an error currently as we drop the iteration types of the first constituent when the second fails.

For other uses we continue to preserve the early exit so that we correctly error for cases such as this:

let data: string[] | null;
const [value] = data; // should still be an error when the second constituent fails.

Fixes #42439

@typescript-bot typescript-bot added Author: Team For Milestone Bug PRs that fix a bug with a specific milestone labels Oct 25, 2023
Andarist added a commit to Andarist/TypeScript that referenced this pull request Oct 26, 2023
@Andarist
Copy link
Contributor

My old PR that awaits review already fixes this issue (I just pushed out a test case that is being added here in this PR). It also shares all of the baseline changes that you have here.

Would you mind re-reviewing that PR and comparing the used approaches? Maybe it would be also good to pull all the other test cases that I added there here to compare their results?

@rbuckton
Copy link
Member Author

My old PR that awaits review already fixes this issue (I just pushed out a test case that is being added here in this PR). It also shares all of the baseline changes that you have here.

Would you mind re-reviewing that PR and comparing the used approaches? Maybe it would be also good to pull all the other test cases that I added there here to compare their results?

The test in tests/cases/compiler/contextuallyTypeAsyncFunctionReturnTypeFromUnion.ts fails in my branch. It looks like your PR covers more cases, so I'll happily close this branch in favor of your PR.

@rbuckton
Copy link
Member Author

Closed in favor of #51196.

@rbuckton rbuckton closed this Oct 26, 2023
@rbuckton rbuckton deleted the fix-42439 branch October 26, 2023 15:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Author: Team For Milestone Bug PRs that fix a bug with a specific milestone
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Union of Generator and AsyncGenerator introduces spurious undefined next() value path
3 participants