Skip to content

Emit error when trying to iterate a value with union type with non-iterable constituent when --downlevelIterator #40288

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 2 commits into from

Conversation

uhyo
Copy link
Contributor

@uhyo uhyo commented Aug 27, 2020

Fixes #39161 .

Example

code

function getArrayOrUndefined() {
  return Math.random() > 0.5 ? [] : undefined;
}

const result = [...getArrayOrUndefined()];
{
  "compilerOptions": {
    "target": "es5",
    "lib": ["es2015"],
    "strict": true,
    "downlevelIteration": true
  }
}

Current Behavior

(no compile error)

New Behavior

src/index.ts:5:20 - error TS2548: Type 'never[] | undefined' is not an array type or does not have a '[Symbol.iterator]()' method that returns an iterator.

5 const result = [...getArrayOrUndefined()];
                     ~~~~~~~~~~~~~~~~~~~~~


Found 1 error.

Details of Code Change

This PR adds a shouldIgnoreNonIterableUnionConstituent parameter to getIterationTypesOfIterable which, if false is specified, returns noIterationTypes whenever given union type includes a non-iterable constituent. Otherwise, non-iterable constituents are just ignored; for example, the yielded type of number[] | undefined is considered number.

I first considered making this the default behavior, but then one test failed (namely generatorYieldContextualType.ts). As the old behavior seemed kind of reasonable in some situations, I kept the change smaller by adding that parameter.

@typescript-bot typescript-bot added the For Backlog Bug PRs that fix a backlog bug label Aug 27, 2020
@sandersn sandersn assigned jakebailey and unassigned elibarzilay Feb 24, 2022
Copy link
Member

@jakebailey jakebailey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems fine to me, but it currently has a merge conflict; can you fix it?

@uhyo
Copy link
Contributor Author

uhyo commented Mar 11, 2022

@jakebailey Thank you very much for noticing this PR!

However, the issue seems to have already been fixed by #40350.

#39161 and this PR can now be closed.

@jakebailey
Copy link
Member

Wow, clearly I didn't note the date, sorry about that!

I tested this manually, and yeah, this is fixed.

@jakebailey jakebailey closed this Mar 11, 2022
@uhyo uhyo deleted the fix-39161 branch March 12, 2022 01:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
For Backlog Bug PRs that fix a backlog bug
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

[Bug] No error on array spread of ([] | undefined) if "downlevelIteration" and "es2015.iterable" are both set
4 participants