@@ -8206,7 +8206,8 @@ namespace ts {
82068206 function isSignatureAssignableTo(source: Signature,
82078207 target: Signature,
82088208 ignoreReturnTypes: boolean): boolean {
8209- return compareSignaturesRelated(source, target, ignoreReturnTypes, /*reportErrors*/ false, /*errorReporter*/ undefined, compareTypesAssignable) !== Ternary.False;
8209+ return compareSignaturesRelated(source, target, /*checkAsCallback*/ false, ignoreReturnTypes, /*reportErrors*/ false,
8210+ /*errorReporter*/ undefined, compareTypesAssignable) !== Ternary.False;
82108211 }
82118212
82128213 type ErrorReporter = (message: DiagnosticMessage, arg0?: string, arg1?: string) => void;
@@ -8216,6 +8217,7 @@ namespace ts {
82168217 */
82178218 function compareSignaturesRelated(source: Signature,
82188219 target: Signature,
8220+ checkAsCallback: boolean,
82198221 ignoreReturnTypes: boolean,
82208222 reportErrors: boolean,
82218223 errorReporter: ErrorReporter,
@@ -8258,9 +8260,23 @@ namespace ts {
82588260 const sourceParams = source.parameters;
82598261 const targetParams = target.parameters;
82608262 for (let i = 0; i < checkCount; i++) {
8261- const s = i < sourceMax ? getTypeOfParameter(sourceParams[i]) : getRestTypeOfSignature(source);
8262- const t = i < targetMax ? getTypeOfParameter(targetParams[i]) : getRestTypeOfSignature(target);
8263- const related = compareTypes(s, t, /*reportErrors*/ false) || compareTypes(t, s, reportErrors);
8263+ const sourceType = i < sourceMax ? getTypeOfParameter(sourceParams[i]) : getRestTypeOfSignature(source);
8264+ const targetType = i < targetMax ? getTypeOfParameter(targetParams[i]) : getRestTypeOfSignature(target);
8265+ const sourceSig = getSingleCallSignature(getNonNullableType(sourceType));
8266+ const targetSig = getSingleCallSignature(getNonNullableType(targetType));
8267+ // In order to ensure that any generic type Foo<T> is at least co-variant with respect to T no matter
8268+ // how Foo uses T, we need to relate parameters bi-variantly (given that parameters are input positions,
8269+ // they naturally relate only contra-variantly). However, if the source and target parameters both have
8270+ // function types with a single call signature, we known we are relating two callback parameters. In
8271+ // that case it is sufficient to only relate the parameters of the signatures co-variantly because,
8272+ // similar to return values, callback parameters are output positions. This means that a Promise<T>,
8273+ // where T is used only in callback parameter positions, will be co-variant (as opposed to bi-variant)
8274+ // with respect to T.
8275+ const callbacks = sourceSig && targetSig && !sourceSig.typePredicate && !targetSig.typePredicate &&
8276+ (getFalsyFlags(sourceType) & TypeFlags.Nullable) === (getFalsyFlags(targetType) & TypeFlags.Nullable);
8277+ const related = callbacks ?
8278+ compareSignaturesRelated(targetSig, sourceSig, /*checkAsCallback*/ true, /*ignoreReturnTypes*/ false, reportErrors, errorReporter, compareTypes) :
8279+ !checkAsCallback && compareTypes(sourceType, targetType, /*reportErrors*/ false) || compareTypes(targetType, sourceType, reportErrors);
82648280 if (!related) {
82658281 if (reportErrors) {
82668282 errorReporter(Diagnostics.Types_of_parameters_0_and_1_are_incompatible,
@@ -8292,7 +8308,11 @@ namespace ts {
82928308 }
82938309 }
82948310 else {
8295- result &= compareTypes(sourceReturnType, targetReturnType, reportErrors);
8311+ // When relating callback signatures, we still need to relate return types bi-variantly as otherwise
8312+ // the containing type wouldn't be co-variant. For example, interface Foo<T> { add(cb: () => T): void }
8313+ // wouldn't be co-variant for T without this rule.
8314+ result &= checkAsCallback && compareTypes(targetReturnType, sourceReturnType, /*reportErrors*/ false) ||
8315+ compareTypes(sourceReturnType, targetReturnType, reportErrors);
82968316 }
82978317
82988318 }
@@ -9260,7 +9280,7 @@ namespace ts {
92609280 * See signatureAssignableTo, compareSignaturesIdentical
92619281 */
92629282 function signatureRelatedTo(source: Signature, target: Signature, reportErrors: boolean): Ternary {
9263- return compareSignaturesRelated(source, target, /*ignoreReturnTypes*/ false, reportErrors, reportError, isRelatedTo);
9283+ return compareSignaturesRelated(source, target, /*checkAsCallback*/ false, /* ignoreReturnTypes*/ false, reportErrors, reportError, isRelatedTo);
92649284 }
92659285
92669286 function signaturesIdenticalTo(source: Type, target: Type, kind: SignatureKind): Ternary {
0 commit comments