@@ -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
}
@@ -13450,7 +13436,8 @@ namespace ts {
13450
13436
if (includeOptional
13451
13437
? !(filteredByApplicability!.flags & TypeFlags.Never)
13452
13438
: isRelatedTo(targetConstraint, sourceKeys)) {
13453
- const indexingType = filteredByApplicability || getTypeParameterFromMappedType(target);
13439
+ const typeParameter = getTypeParameterFromMappedType(target);
13440
+ const indexingType = filteredByApplicability ? getIntersectionType([filteredByApplicability, typeParameter]) : typeParameter;
13454
13441
const indexedAccessType = getIndexedAccessType(source, indexingType);
13455
13442
const templateType = getTemplateTypeFromMappedType(target);
13456
13443
if (result = isRelatedTo(indexedAccessType, templateType, reportErrors)) {
@@ -15265,15 +15252,23 @@ namespace ts {
15265
15252
const inference = inferences[i];
15266
15253
if (t === inference.typeParameter) {
15267
15254
if (fix && !inference.isFixed) {
15255
+ clearCachedInferences(inferences);
15268
15256
inference.isFixed = true;
15269
- inference.inferredType = undefined;
15270
15257
}
15271
15258
return getInferredType(context, i);
15272
15259
}
15273
15260
}
15274
15261
return t;
15275
15262
}
15276
15263
15264
+ function clearCachedInferences(inferences: InferenceInfo[]) {
15265
+ for (const inference of inferences) {
15266
+ if (!inference.isFixed) {
15267
+ inference.inferredType = undefined;
15268
+ }
15269
+ }
15270
+ }
15271
+
15277
15272
function createInferenceInfo(typeParameter: TypeParameter): InferenceInfo {
15278
15273
return {
15279
15274
typeParameter,
@@ -15553,17 +15548,17 @@ namespace ts {
15553
15548
if (contravariant && !bivariant) {
15554
15549
if (!contains(inference.contraCandidates, candidate)) {
15555
15550
inference.contraCandidates = append(inference.contraCandidates, candidate);
15556
- inference.inferredType = undefined ;
15551
+ clearCachedInferences(inferences) ;
15557
15552
}
15558
15553
}
15559
15554
else if (!contains(inference.candidates, candidate)) {
15560
15555
inference.candidates = append(inference.candidates, candidate);
15561
- inference.inferredType = undefined ;
15556
+ clearCachedInferences(inferences) ;
15562
15557
}
15563
15558
}
15564
15559
if (!(priority & InferencePriority.ReturnType) && target.flags & TypeFlags.TypeParameter && inference.topLevel && !isTypeParameterAtTopLevel(originalTarget, <TypeParameter>target)) {
15565
15560
inference.topLevel = false;
15566
- inference.inferredType = undefined ;
15561
+ clearCachedInferences(inferences) ;
15567
15562
}
15568
15563
}
15569
15564
return;
@@ -20714,7 +20709,8 @@ namespace ts {
20714
20709
else {
20715
20710
const promisedType = getPromisedTypeOfPromise(containingType);
20716
20711
if (promisedType && getPropertyOfType(promisedType, propNode.escapedText)) {
20717
- errorInfo = chainDiagnosticMessages(errorInfo, Diagnostics.Property_0_does_not_exist_on_type_1_Did_you_forget_to_use_await, declarationNameToString(propNode), typeToString(containingType));
20712
+ errorInfo = chainDiagnosticMessages(errorInfo, Diagnostics.Property_0_does_not_exist_on_type_1, declarationNameToString(propNode), typeToString(containingType));
20713
+ relatedInfo = createDiagnosticForNode(propNode, Diagnostics.Did_you_forget_to_use_await);
20718
20714
}
20719
20715
else {
20720
20716
const suggestion = getSuggestedSymbolForNonexistentProperty(propNode, containingType);
@@ -22748,7 +22744,7 @@ namespace ts {
22748
22744
isVariableDeclaration(decl.parent) && getSymbolOfNode(decl.parent));
22749
22745
const prototype = assignmentSymbol && assignmentSymbol.exports && assignmentSymbol.exports.get("prototype" as __String);
22750
22746
const init = prototype && prototype.valueDeclaration && getAssignedJSPrototype(prototype.valueDeclaration);
22751
- return init ? checkExpression( init) : undefined;
22747
+ return init ? getWidenedType(checkExpressionCached( init) ) : undefined;
22752
22748
}
22753
22749
22754
22750
function getAssignedJSPrototype(node: Node) {
@@ -23391,9 +23387,36 @@ namespace ts {
23391
23387
}
23392
23388
23393
23389
function createGeneratorReturnType(yieldType: Type, returnType: Type, nextType: Type, isAsyncGenerator: boolean) {
23394
- return isAsyncGenerator
23395
- ? createAsyncGeneratorType(yieldType, returnType, nextType)
23396
- : 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]);
23397
23420
}
23398
23421
23399
23422
function checkAndAggregateYieldOperandTypes(func: FunctionLikeDeclaration, checkMode: CheckMode | undefined) {
@@ -26401,7 +26424,11 @@ namespace ts {
26401
26424
* The runtime behavior of the `await` keyword.
26402
26425
*/
26403
26426
function checkAwaitedType(type: Type, errorNode: Node, diagnosticMessage: DiagnosticMessage, arg0?: string | number): Type {
26404
- return getAwaitedType(type, errorNode, diagnosticMessage, arg0) || errorType;
26427
+ const awaitedType = getAwaitedType(type, errorNode, diagnosticMessage, arg0);
26428
+ if (awaitedType === type && !(type.flags & TypeFlags.AnyOrUnknown)) {
26429
+ addErrorOrSuggestion(/*isError*/ false, createDiagnosticForNode(errorNode, Diagnostics.await_has_no_effect_on_the_type_of_this_expression));
26430
+ }
26431
+ return awaitedType || errorType;
26405
26432
}
26406
26433
26407
26434
function getAwaitedType(type: Type, errorNode?: Node, diagnosticMessage?: DiagnosticMessage, arg0?: string | number): Type | undefined {
@@ -28226,6 +28253,13 @@ namespace ts {
28226
28253
return (type as IterableOrIteratorType)[resolver.iterableCacheKey];
28227
28254
}
28228
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
+
28229
28263
/**
28230
28264
* Gets the *yield*, *return*, and *next* types of an `Iterable`-like or `AsyncIterable`-like
28231
28265
* type from from common heuristics.
@@ -28251,10 +28285,7 @@ namespace ts {
28251
28285
// iteration types of their `[Symbol.iterator]()` method. The same is true for their async cousins.
28252
28286
// While we define these as `any` and `undefined` in our libs by default, a custom lib *could* use
28253
28287
// different definitions.
28254
- const globalIterationTypes =
28255
- getIterationTypesOfIterableCached(globalType, resolver) ||
28256
- getIterationTypesOfIterableSlow(globalType, resolver, /*errorNode*/ undefined);
28257
- const { returnType, nextType } = globalIterationTypes === noIterationTypes ? defaultIterationTypes : globalIterationTypes;
28288
+ const { returnType, nextType } = getIterationTypesOfGlobalIterableType(globalType, resolver);
28258
28289
return (type as IterableOrIteratorType)[resolver.iterableCacheKey] = createIterationTypes(yieldType, returnType, nextType);
28259
28290
}
28260
28291
@@ -33011,9 +33042,6 @@ namespace ts {
33011
33042
return grammarErrorAtPos(node, node.end - 1, ";".length, Diagnostics._0_expected, "{");
33012
33043
}
33013
33044
}
33014
- else if (isClassLike(node.parent) && isStringLiteral(node.name) && node.name.text === "constructor" && (!compilerOptions.target || compilerOptions.target < ScriptTarget.ES5)) {
33015
- return grammarErrorOnNode(node.name, Diagnostics.Quoted_constructors_have_previously_been_interpreted_as_methods_which_is_incorrect_In_TypeScript_3_6_they_will_be_correctly_parsed_as_constructors_In_the_meantime_consider_using_constructor_to_write_a_constructor_or_constructor_to_write_a_method);
33016
- }
33017
33045
if (checkGrammarForGenerator(node)) {
33018
33046
return true;
33019
33047
}
0 commit comments