X Tutup
The Wayback Machine - https://web.archive.org/web/20220126145136/https://github.com/microsoft/TypeScript/pull/40288
Skip to content
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

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
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.

@sandersn sandersn added this to Not started in PR Backlog Sep 8, 2020
@sandersn sandersn moved this from Not started to Needs review in PR Backlog Sep 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
PR Backlog
  
Waiting on reviewers
Linked issues

Successfully merging this pull request may close these issues.

3 participants
X Tutup