* When consuming an iterable type in a for..of, spread, or iterator destructuring assignment * we want to get the iterated type of an iterable for ES2015 or later, or the iterated type * of a iterable (if defined globally) or element type of an array like for ES2015 or earlier.
(use, inputType, sentType, errorNode, checkAssignability)
| 82960 | * of a iterable (if defined globally) or element type of an array like for ES2015 or earlier. |
| 82961 | */ |
| 82962 | function getIteratedTypeOrElementType(use, inputType, sentType, errorNode, checkAssignability) { |
| 82963 | var allowAsyncIterables = (use & 2 /* IterationUse.AllowsAsyncIterablesFlag */) !== 0; |
| 82964 | if (inputType === neverType) { |
| 82965 | reportTypeNotIterableError(errorNode, inputType, allowAsyncIterables); // TODO: GH#18217 |
| 82966 | return undefined; |
| 82967 | } |
| 82968 | var uplevelIteration = languageVersion >= 2 /* ScriptTarget.ES2015 */; |
| 82969 | var downlevelIteration = !uplevelIteration && compilerOptions.downlevelIteration; |
| 82970 | var possibleOutOfBounds = compilerOptions.noUncheckedIndexedAccess && !!(use & 128 /* IterationUse.PossiblyOutOfBounds */); |
| 82971 | // Get the iterated type of an `Iterable<T>` or `IterableIterator<T>` only in ES2015 |
| 82972 | // or higher, when inside of an async generator or for-await-if, or when |
| 82973 | // downlevelIteration is requested. |
| 82974 | if (uplevelIteration || downlevelIteration || allowAsyncIterables) { |
| 82975 | // We only report errors for an invalid iterable type in ES2015 or higher. |
| 82976 | var iterationTypes = getIterationTypesOfIterable(inputType, use, uplevelIteration ? errorNode : undefined); |
| 82977 | if (checkAssignability) { |
| 82978 | if (iterationTypes) { |
| 82979 | var diagnostic = use & 8 /* IterationUse.ForOfFlag */ ? ts.Diagnostics.Cannot_iterate_value_because_the_next_method_of_its_iterator_expects_type_1_but_for_of_will_always_send_0 : |
| 82980 | use & 32 /* IterationUse.SpreadFlag */ ? ts.Diagnostics.Cannot_iterate_value_because_the_next_method_of_its_iterator_expects_type_1_but_array_spread_will_always_send_0 : |
| 82981 | use & 64 /* IterationUse.DestructuringFlag */ ? ts.Diagnostics.Cannot_iterate_value_because_the_next_method_of_its_iterator_expects_type_1_but_array_destructuring_will_always_send_0 : |
| 82982 | use & 16 /* IterationUse.YieldStarFlag */ ? ts.Diagnostics.Cannot_delegate_iteration_to_value_because_the_next_method_of_its_iterator_expects_type_1_but_the_containing_generator_will_always_send_0 : |
| 82983 | undefined; |
| 82984 | if (diagnostic) { |
| 82985 | checkTypeAssignableTo(sentType, iterationTypes.nextType, errorNode, diagnostic); |
| 82986 | } |
| 82987 | } |
| 82988 | } |
| 82989 | if (iterationTypes || uplevelIteration) { |
| 82990 | return possibleOutOfBounds ? includeUndefinedInIndexSignature(iterationTypes && iterationTypes.yieldType) : (iterationTypes && iterationTypes.yieldType); |
| 82991 | } |
| 82992 | } |
| 82993 | var arrayType = inputType; |
| 82994 | var reportedError = false; |
| 82995 | var hasStringConstituent = false; |
| 82996 | // If strings are permitted, remove any string-like constituents from the array type. |
| 82997 | // This allows us to find other non-string element types from an array unioned with |
| 82998 | // a string. |
| 82999 | if (use & 4 /* IterationUse.AllowsStringInputFlag */) { |
| 83000 | if (arrayType.flags & 1048576 /* TypeFlags.Union */) { |
| 83001 | // After we remove all types that are StringLike, we will know if there was a string constituent |
| 83002 | // based on whether the result of filter is a new array. |
| 83003 | var arrayTypes = inputType.types; |
| 83004 | var filteredTypes = ts.filter(arrayTypes, function (t) { return !(t.flags & 402653316 /* TypeFlags.StringLike */); }); |
| 83005 | if (filteredTypes !== arrayTypes) { |
| 83006 | arrayType = getUnionType(filteredTypes, 2 /* UnionReduction.Subtype */); |
| 83007 | } |
| 83008 | } |
| 83009 | else if (arrayType.flags & 402653316 /* TypeFlags.StringLike */) { |
| 83010 | arrayType = neverType; |
| 83011 | } |
| 83012 | hasStringConstituent = arrayType !== inputType; |
| 83013 | if (hasStringConstituent) { |
| 83014 | if (languageVersion < 1 /* ScriptTarget.ES5 */) { |
| 83015 | if (errorNode) { |
| 83016 | error(errorNode, ts.Diagnostics.Using_a_string_in_a_for_of_statement_is_only_supported_in_ECMAScript_5_and_higher); |
| 83017 | reportedError = true; |
| 83018 | } |
| 83019 | } |
no test coverage detected
searching dependent graphs…