From dc2af2fb7fd1ca54e05c8cc211d05041b23dc708 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Tue, 8 Dec 2015 17:08:23 -0800 Subject: [PATCH] Use different relation for overload compatibility. --- src/compiler/checker.ts | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 30d4818592b..212d863e575 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -4959,6 +4959,25 @@ namespace ts { return checkTypeRelatedTo(sourceType, targetType, assignableRelation, /*errorNode*/ undefined); } + function isImplementationCompatibleWithOverload(implementation: Signature, overload: Signature): boolean { + const erasedSource = getErasedSignature(implementation); + const erasedTarget = getErasedSignature(overload); + + const sourceReturnType = getReturnTypeOfSignature(erasedSource); + const targetReturnType = getReturnTypeOfSignature(erasedTarget); + if (checkTypeRelatedTo(sourceReturnType, targetReturnType, assignableRelation, /*errorNode*/ undefined) + || checkTypeRelatedTo(targetReturnType, sourceReturnType, assignableRelation, /*errorNode*/ undefined)) { + const anyReturningSource = cloneSignature(erasedSource); + const anyReturningTarget = cloneSignature(erasedTarget); + anyReturningSource.resolvedReturnType = anyType; + anyReturningTarget.resolvedReturnType = anyType; + + const anyReturningSourceType = getOrCreateTypeFromSignature(anyReturningSource); + const anyReturningTargetType = getOrCreateTypeFromSignature(anyReturningTarget); + return checkTypeRelatedTo(anyReturningSourceType, anyReturningTargetType, assignableRelation, /*errorNode*/ undefined); + } + } + /** * Checks if 'source' is related to 'target' (e.g.: is a assignable to). * @param source The left-hand-side of the relation. @@ -11683,7 +11702,7 @@ namespace ts { // // The implementation is completely unrelated to the specialized signature, yet we do not check this. for (const signature of signatures) { - if (!signature.hasStringLiterals && !isSignatureAssignableTo(bodySignature, signature)) { + if (!signature.hasStringLiterals && !isImplementationCompatibleWithOverload(bodySignature, signature)) { error(signature.declaration, Diagnostics.Overload_signature_is_not_compatible_with_function_implementation); break; }