Skip to content

Commit

Permalink
Allow callbacks unioned with null and/or undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
ahejlsberg committed Apr 11, 2017
1 parent 510bc81 commit ec35b80
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/compiler/checker.ts
Expand Up @@ -8213,8 +8213,8 @@ namespace ts {
for (let i = 0; i < checkCount; i++) {
const sourceType = i < sourceMax ? getTypeOfParameter(sourceParams[i]) : getRestTypeOfSignature(source);
const targetType = i < targetMax ? getTypeOfParameter(targetParams[i]) : getRestTypeOfSignature(target);
const sourceSig = getSingleCallSignature(sourceType);
const targetSig = getSingleCallSignature(targetType);
const sourceSig = getSingleCallSignature(getNonNullableType(sourceType));
const targetSig = getSingleCallSignature(getNonNullableType(targetType));
// In order to ensure that any generic type Foo<T> is at least co-variant with respect to T no matter
// how Foo uses T, we need to relate parameters bi-variantly (given that parameters are input positions,
// they naturally relate only contra-variantly). However, if the source and target parameters both have
Expand All @@ -8223,7 +8223,9 @@ namespace ts {
// similar to return values, callback parameters are output positions. This means that a Promise<T>,
// where T is used only in callback parameter positions, will be co-variant (as opposed to bi-variant)
// with respect to T.
const related = sourceSig && targetSig && !sourceSig.typePredicate && !targetSig.typePredicate ?
const callbacks = sourceSig && targetSig && !sourceSig.typePredicate && !targetSig.typePredicate &&
(getFalsyFlags(sourceType) & TypeFlags.Nullable) === (getFalsyFlags(targetType) & TypeFlags.Nullable);
const related = callbacks ?
compareSignaturesRelated(targetSig, sourceSig, /*checkAsCallback*/ true, /*ignoreReturnTypes*/ false, reportErrors, errorReporter, compareTypes) :
!checkAsCallback && compareTypes(sourceType, targetType, /*reportErrors*/ false) || compareTypes(targetType, sourceType, reportErrors);
if (!related) {
Expand Down

0 comments on commit ec35b80

Please sign in to comment.