@@ -21088,7 +21088,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
2108821088 }
2108921089
2109021090 function hasContextSensitiveReturnExpression(node: FunctionLikeDeclaration) {
21091- if (getEffectiveReturnTypeNode(node) || !node.body) {
21091+ if (node.typeParameters || getEffectiveReturnTypeNode(node) || !node.body) {
2109221092 return false;
2109321093 }
2109421094 if (node.body.kind !== SyntaxKind.Block) {
@@ -26067,7 +26067,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
2606726067 });
2606826068 }
2606926069
26070- function applyToParameterTypes(source: Signature, target: Signature, callback: (s: Type, t: Type) => void) {
26070+ function applyToParameterTypes(source: Signature, target: Signature, callback: (s: Type, t: Type) => void, skipUnannotatedParameters = false) {
26071+ const sourceDeclaredCount = source.parameters.length - (signatureHasRestParameter(source) ? 1 : 0);
2607126072 const sourceCount = getParameterCount(source);
2607226073 const targetCount = getParameterCount(target);
2607326074 const sourceRestType = getEffectiveRestType(source);
@@ -26082,6 +26083,12 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
2608226083 }
2608326084 }
2608426085 for (let i = 0; i < paramCount; i++) {
26086+ if (skipUnannotatedParameters) {
26087+ const decl = i < sourceDeclaredCount ? source.parameters[i] : signatureHasRestParameter(source) ? source.parameters[sourceDeclaredCount] : undefined;
26088+ if (decl?.valueDeclaration && !getEffectiveTypeAnnotationNode(decl.valueDeclaration)) {
26089+ continue
26090+ }
26091+ }
2608526092 callback(getTypeAtPosition(source, i), getTypeAtPosition(target, i));
2608626093 }
2608726094 if (targetRestType) {
@@ -41376,7 +41383,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
4137641383 const inferences = map(context.inferences, info => createInferenceInfo(info.typeParameter));
4137741384 applyToParameterTypes(instantiatedSignature, contextualSignature, (source, target) => {
4137841385 inferTypes(inferences, source, target, /*priority*/ 0, /*contravariant*/ true);
41379- });
41386+ }, /*skipUnannotatedParameters*/ true );
4138041387 if (some(inferences, hasInferenceCandidates)) {
4138141388 // We have inference candidates, indicating that one or more type parameters are referenced
4138241389 // in the parameter types of the contextual signature. Now also infer from the return type.
@@ -41389,6 +41396,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
4138941396 if (!hasOverlappingInferences(context.inferences, inferences)) {
4139041397 mergeInferences(context.inferences, inferences);
4139141398 context.inferredTypeParameters = concatenate(context.inferredTypeParameters, uniqueTypeParameters);
41399+ assignContextualParameterTypes(signature, instantiateSignature(contextualSignature, context.mapper));
4139241400 return getOrCreateTypeFromSignature(instantiatedSignature);
4139341401 }
4139441402 }
@@ -41812,7 +41820,13 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
4181241820 // or if its FunctionBody is strict code(11.1.5).
4181341821 checkGrammarModifiers(node);
4181441822
41815- checkVariableLikeDeclaration(node);
41823+ if (getEffectiveTypeAnnotationNode(node)) {
41824+ // checking annotated parameters early allows the compiler to find circularties early
41825+ checkVariableLikeDeclaration(node);
41826+ } else {
41827+ // defer resolving the type of unannotated parameters so that late contextual parameter types can be assigned before it
41828+ checkNodeDeferred(node);
41829+ }
4181641830 const func = getContainingFunction(node)!;
4181741831 if (hasSyntacticModifier(node, ModifierFlags.ParameterPropertyModifier)) {
4181841832 if (compilerOptions.erasableSyntaxOnly) {
@@ -49158,6 +49172,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
4915849172 case SyntaxKind.ClassExpression:
4915949173 checkClassExpressionDeferred(node as ClassExpression);
4916049174 break;
49175+ case SyntaxKind.Parameter:
49176+ checkVariableLikeDeclaration(node as ParameterDeclaration);
49177+ break;
4916149178 case SyntaxKind.TypeParameter:
4916249179 checkTypeParameterDeferred(node as TypeParameterDeclaration);
4916349180 break;
0 commit comments