@@ -42,10 +42,10 @@ namespace ts {
42
42
iterableCacheKey: "iterationTypesOfAsyncIterable" | "iterationTypesOfIterable";
43
43
iteratorCacheKey: "iterationTypesOfAsyncIterator" | "iterationTypesOfIterator";
44
44
iteratorSymbolName: "asyncIterator" | "iterator";
45
- getGlobalIteratorType: (reportErrors: boolean) => Type ;
46
- getGlobalIterableType: (reportErrors: boolean) => Type ;
47
- getGlobalIterableIteratorType: (reportErrors: boolean) => Type ;
48
- getGlobalGeneratorType: (reportErrors: boolean) => Type ;
45
+ getGlobalIteratorType: (reportErrors: boolean) => GenericType ;
46
+ getGlobalIterableType: (reportErrors: boolean) => GenericType ;
47
+ getGlobalIterableIteratorType: (reportErrors: boolean) => GenericType ;
48
+ getGlobalGeneratorType: (reportErrors: boolean) => GenericType ;
49
49
resolveIterationType: (type: Type, errorNode: Node | undefined) => Type | undefined;
50
50
mustHaveANextMethodDiagnostic: DiagnosticMessage;
51
51
mustBeAMethodDiagnostic: DiagnosticMessage;
@@ -9531,24 +9531,10 @@ namespace ts {
9531
9531
return createTypeFromGenericGlobalType(getGlobalTypedPropertyDescriptorType(), [propertyType]);
9532
9532
}
9533
9533
9534
- function createAsyncGeneratorType(yieldType: Type, returnType: Type, nextType: Type) {
9535
- const globalAsyncGeneratorType = getGlobalAsyncGeneratorType(/*reportErrors*/ true);
9536
- if (globalAsyncGeneratorType !== emptyGenericType) {
9537
- yieldType = getAwaitedType(yieldType) || unknownType;
9538
- returnType = getAwaitedType(returnType) || unknownType;
9539
- nextType = getAwaitedType(nextType) || unknownType;
9540
- }
9541
- return createTypeFromGenericGlobalType(globalAsyncGeneratorType, [yieldType, returnType, nextType]);
9542
- }
9543
-
9544
9534
function createIterableType(iteratedType: Type): Type {
9545
9535
return createTypeFromGenericGlobalType(getGlobalIterableType(/*reportErrors*/ true), [iteratedType]);
9546
9536
}
9547
9537
9548
- function createGeneratorType(yieldType: Type, returnType: Type, nextType: Type) {
9549
- return createTypeFromGenericGlobalType(getGlobalGeneratorType(/*reportErrors*/ true), [yieldType, returnType, nextType]);
9550
- }
9551
-
9552
9538
function createArrayType(elementType: Type, readonly?: boolean): ObjectType {
9553
9539
return createTypeFromGenericGlobalType(readonly ? globalReadonlyArrayType : globalArrayType, [elementType]);
9554
9540
}
@@ -23401,9 +23387,36 @@ namespace ts {
23401
23387
}
23402
23388
23403
23389
function createGeneratorReturnType(yieldType: Type, returnType: Type, nextType: Type, isAsyncGenerator: boolean) {
23404
- return isAsyncGenerator
23405
- ? createAsyncGeneratorType(yieldType, returnType, nextType)
23406
- : createGeneratorType(yieldType, returnType, nextType);
23390
+ const resolver = isAsyncGenerator ? asyncIterationTypesResolver : syncIterationTypesResolver;
23391
+ const globalGeneratorType = resolver.getGlobalGeneratorType(/*reportErrors*/ false);
23392
+ yieldType = resolver.resolveIterationType(yieldType, /*errorNode*/ undefined) || unknownType;
23393
+ returnType = resolver.resolveIterationType(returnType, /*errorNode*/ undefined) || unknownType;
23394
+ nextType = resolver.resolveIterationType(nextType, /*errorNode*/ undefined) || unknownType;
23395
+ if (globalGeneratorType === emptyGenericType) {
23396
+ // Fall back to the global IterableIterator if returnType is assignable to the expected return iteration
23397
+ // type of IterableIterator, and the expected next iteration type of IterableIterator is assignable to
23398
+ // nextType.
23399
+ const globalType = resolver.getGlobalIterableIteratorType(/*reportErrors*/ false);
23400
+ const iterationTypes = globalType !== emptyGenericType ? getIterationTypesOfGlobalIterableType(globalType, resolver) : undefined;
23401
+ const iterableIteratorReturnType = iterationTypes ? iterationTypes.returnType : anyType;
23402
+ const iterableIteratorNextType = iterationTypes ? iterationTypes.nextType : undefinedType;
23403
+ if (isTypeAssignableTo(returnType, iterableIteratorReturnType) &&
23404
+ isTypeAssignableTo(iterableIteratorNextType, nextType)) {
23405
+ if (globalType !== emptyGenericType) {
23406
+ return createTypeFromGenericGlobalType(globalType, [yieldType]);
23407
+ }
23408
+
23409
+ // The global IterableIterator type doesn't exist, so report an error
23410
+ resolver.getGlobalIterableIteratorType(/*reportErrors*/ true);
23411
+ return emptyObjectType;
23412
+ }
23413
+
23414
+ // The global Generator type doesn't exist, so report an error
23415
+ resolver.getGlobalGeneratorType(/*reportErrors*/ true);
23416
+ return emptyObjectType;
23417
+ }
23418
+
23419
+ return createTypeFromGenericGlobalType(globalGeneratorType, [yieldType, returnType, nextType]);
23407
23420
}
23408
23421
23409
23422
function checkAndAggregateYieldOperandTypes(func: FunctionLikeDeclaration, checkMode: CheckMode | undefined) {
@@ -28240,6 +28253,13 @@ namespace ts {
28240
28253
return (type as IterableOrIteratorType)[resolver.iterableCacheKey];
28241
28254
}
28242
28255
28256
+ function getIterationTypesOfGlobalIterableType(globalType: Type, resolver: IterationTypesResolver) {
28257
+ const globalIterationTypes =
28258
+ getIterationTypesOfIterableCached(globalType, resolver) ||
28259
+ getIterationTypesOfIterableSlow(globalType, resolver, /*errorNode*/ undefined);
28260
+ return globalIterationTypes === noIterationTypes ? defaultIterationTypes : globalIterationTypes;
28261
+ }
28262
+
28243
28263
/**
28244
28264
* Gets the *yield*, *return*, and *next* types of an `Iterable`-like or `AsyncIterable`-like
28245
28265
* type from from common heuristics.
@@ -28265,10 +28285,7 @@ namespace ts {
28265
28285
// iteration types of their `[Symbol.iterator]()` method. The same is true for their async cousins.
28266
28286
// While we define these as `any` and `undefined` in our libs by default, a custom lib *could* use
28267
28287
// different definitions.
28268
- const globalIterationTypes =
28269
- getIterationTypesOfIterableCached(globalType, resolver) ||
28270
- getIterationTypesOfIterableSlow(globalType, resolver, /*errorNode*/ undefined);
28271
- const { returnType, nextType } = globalIterationTypes === noIterationTypes ? defaultIterationTypes : globalIterationTypes;
28288
+ const { returnType, nextType } = getIterationTypesOfGlobalIterableType(globalType, resolver);
28272
28289
return (type as IterableOrIteratorType)[resolver.iterableCacheKey] = createIterationTypes(yieldType, returnType, nextType);
28273
28290
}
28274
28291
0 commit comments