@@ -8177,7 +8177,7 @@ namespace ts {
81778177 function isSignatureAssignableTo(source: Signature,
81788178 target: Signature,
81798179 ignoreReturnTypes: boolean): boolean {
8180- return compareSignaturesRelated(source, target, /*checkAsCallback */ false, ignoreReturnTypes, /*reportErrors*/ false,
8180+ return compareSignaturesRelated(source, target, /*strictVariance */ false, ignoreReturnTypes, /*reportErrors*/ false,
81818181 /*errorReporter*/ undefined, compareTypesAssignable) !== Ternary.False;
81828182 }
81838183
@@ -8188,7 +8188,7 @@ namespace ts {
81888188 */
81898189 function compareSignaturesRelated(source: Signature,
81908190 target: Signature,
8191- checkAsCallback : boolean,
8191+ strictVariance : boolean,
81928192 ignoreReturnTypes: boolean,
81938193 reportErrors: boolean,
81948194 errorReporter: ErrorReporter,
@@ -8235,17 +8235,13 @@ namespace ts {
82358235 const targetType = i < targetMax ? getTypeOfParameter(targetParams[i]) : getRestTypeOfSignature(target);
82368236 const sourceSig = getSingleCallSignature(sourceType);
82378237 const targetSig = getSingleCallSignature(targetType);
8238- // In order to ensure that any generic type Foo<T> is at least co-variant with respect to T no matter
8239- // how Foo uses T, we need to relate parameters bi-variantly (given that parameters are input positions,
8240- // they naturally relate only contra-variantly). However, if the source and target parameters both have
8241- // function types with a single call signature, we known we are relating two callback parameters. In
8242- // that case it is sufficient to only relate the parameters of the signatures co-variantly because,
8243- // similar to return values, callback parameters are output positions. This means that a Promise<T>,
8244- // where T is used only in callback parameter positions, will be co-variant (as opposed to bi-variant)
8245- // with respect to T.
8246- const related = sourceSig && targetSig && !sourceSig.typePredicate && !targetSig.typePredicate ?
8247- compareSignaturesRelated(targetSig, sourceSig, /*checkAsCallback*/ true, /*ignoreReturnTypes*/ false, reportErrors, errorReporter, compareTypes) :
8248- !checkAsCallback && compareTypes(sourceType, targetType, /*reportErrors*/ false) || compareTypes(targetType, sourceType, reportErrors);
8238+ // If the source and target parameters both have function types with a single call signature we are
8239+ // relating two callback parameters. In that case we compare the callback signatures with strict
8240+ // variance, meaning we require the callback parameters to be pairwise co-variant (because, similar
8241+ // to return values, callback parameters are output positions).
8242+ const related = sourceSig && targetSig ?
8243+ compareSignaturesRelated(targetSig, sourceSig, /*strictVariance*/ true, /*ignoreReturnTypes*/ false, reportErrors, errorReporter, compareTypes) :
8244+ !strictVariance && compareTypes(sourceType, targetType, /*reportErrors*/ false) || compareTypes(targetType, sourceType, reportErrors);
82498245 if (!related) {
82508246 if (reportErrors) {
82518247 errorReporter(Diagnostics.Types_of_parameters_0_and_1_are_incompatible,
@@ -8277,11 +8273,7 @@ namespace ts {
82778273 }
82788274 }
82798275 else {
8280- // When relating callback signatures, we still need to relate return types bi-variantly as otherwise
8281- // the containing type wouldn't be co-variant. For example, interface Foo<T> { add(cb: () => T): void }
8282- // wouldn't be co-variant for T without this rule.
8283- result &= checkAsCallback && compareTypes(targetReturnType, sourceReturnType, /*reportErrors*/ false) ||
8284- compareTypes(sourceReturnType, targetReturnType, reportErrors);
8276+ result &= compareTypes(sourceReturnType, targetReturnType, reportErrors);
82858277 }
82868278
82878279 }
@@ -9249,7 +9241,7 @@ namespace ts {
92499241 * See signatureAssignableTo, compareSignaturesIdentical
92509242 */
92519243 function signatureRelatedTo(source: Signature, target: Signature, reportErrors: boolean): Ternary {
9252- return compareSignaturesRelated(source, target, /*checkAsCallback */ false, /*ignoreReturnTypes*/ false, reportErrors, reportError, isRelatedTo);
9244+ return compareSignaturesRelated(source, target, /*strictVariance */ false, /*ignoreReturnTypes*/ false, reportErrors, reportError, isRelatedTo);
92539245 }
92549246
92559247 function signaturesIdenticalTo(source: Type, target: Type, kind: SignatureKind): Ternary {
0 commit comments