Skip to content

Commit 08cd0b3

Browse files
committed
Use proper variances when inferring between type alias instantiations
1 parent c5c869f commit 08cd0b3

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

src/compiler/checker.ts

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15171,11 +15171,7 @@ namespace ts {
1517115171
if (source.aliasSymbol && source.aliasTypeArguments && source.aliasSymbol === target.aliasSymbol) {
1517215172
// Source and target are types originating in the same generic type alias declaration.
1517315173
// Simply infer from source type arguments to target type arguments.
15174-
const sourceTypes = source.aliasTypeArguments;
15175-
const targetTypes = target.aliasTypeArguments!;
15176-
for (let i = 0; i < sourceTypes.length; i++) {
15177-
inferFromTypes(sourceTypes[i], targetTypes[i]);
15178-
}
15174+
inferFromTypeArguments(source.aliasTypeArguments, target.aliasTypeArguments!, getAliasVariances(source.aliasSymbol));
1517915175
return;
1518015176
}
1518115177
if (source.flags & TypeFlags.Union && target.flags & TypeFlags.Union && !(source.flags & TypeFlags.EnumLiteral && target.flags & TypeFlags.EnumLiteral) ||
@@ -15281,18 +15277,7 @@ namespace ts {
1528115277
}
1528215278
if (getObjectFlags(source) & ObjectFlags.Reference && getObjectFlags(target) & ObjectFlags.Reference && (<TypeReference>source).target === (<TypeReference>target).target) {
1528315279
// If source and target are references to the same generic type, infer from type arguments
15284-
const sourceTypes = (<TypeReference>source).typeArguments || emptyArray;
15285-
const targetTypes = (<TypeReference>target).typeArguments || emptyArray;
15286-
const count = sourceTypes.length < targetTypes.length ? sourceTypes.length : targetTypes.length;
15287-
const variances = getVariances((<TypeReference>source).target);
15288-
for (let i = 0; i < count; i++) {
15289-
if (i < variances.length && (variances[i] & VarianceFlags.VarianceMask) === VarianceFlags.Contravariant) {
15290-
inferFromContravariantTypes(sourceTypes[i], targetTypes[i]);
15291-
}
15292-
else {
15293-
inferFromTypes(sourceTypes[i], targetTypes[i]);
15294-
}
15295-
}
15280+
inferFromTypeArguments((<TypeReference>source).typeArguments || emptyArray, (<TypeReference>target).typeArguments || emptyArray, getVariances((<TypeReference>source).target));
1529615281
}
1529715282
else if (source.flags & TypeFlags.Index && target.flags & TypeFlags.Index) {
1529815283
contravariant = !contravariant;
@@ -15412,6 +15397,18 @@ namespace ts {
1541215397
}
1541315398
}
1541415399

15400+
function inferFromTypeArguments(sourceTypes: readonly Type[], targetTypes: readonly Type[], variances: readonly VarianceFlags[]) {
15401+
const count = sourceTypes.length < targetTypes.length ? sourceTypes.length : targetTypes.length;
15402+
for (let i = 0; i < count; i++) {
15403+
if (i < variances.length && (variances[i] & VarianceFlags.VarianceMask) === VarianceFlags.Contravariant) {
15404+
inferFromContravariantTypes(sourceTypes[i], targetTypes[i]);
15405+
}
15406+
else {
15407+
inferFromTypes(sourceTypes[i], targetTypes[i]);
15408+
}
15409+
}
15410+
}
15411+
1541515412
function inferFromContravariantTypes(source: Type, target: Type) {
1541615413
if (strictFunctionTypes || priority & InferencePriority.AlwaysStrict) {
1541715414
contravariant = !contravariant;

0 commit comments

Comments
 (0)