Skip to content

Commit b42c6b1

Browse files
committed
Only conditional types that check naked type parameter distribute over unions
1 parent 4c7ec3c commit b42c6b1

1 file changed

Lines changed: 3 additions & 7 deletions

File tree

src/compiler/checker.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8092,10 +8092,6 @@ namespace ts {
80928092
}
80938093

80948094
function getConditionalType(checkType: Type, extendsType: Type, baseTrueType: Type, baseFalseType: Type, target: ConditionalType, mapper: TypeMapper, aliasSymbol?: Symbol, baseAliasTypeArguments?: Type[]): Type {
8095-
// Distribute union types over conditional types
8096-
if (checkType.flags & TypeFlags.Union) {
8097-
return getUnionType(map((<UnionType>checkType).types, t => getConditionalType(t, extendsType, baseTrueType, baseFalseType, target, mapper)));
8098-
}
80998095
// Return union of trueType and falseType for any and never since they match anything
81008096
if (checkType.flags & (TypeFlags.Any | TypeFlags.Never)) {
81018097
return getUnionType([instantiateType(baseTrueType, mapper), instantiateType(baseFalseType, mapper)]);
@@ -8105,7 +8101,7 @@ namespace ts {
81058101
return instantiateType(baseTrueType, mapper);
81068102
}
81078103
// Return falseType for a definitely false extends check
8108-
if (!typeMaybeAssignableTo(instantiateType(checkType, anyMapper), instantiateType(extendsType, constraintMapper))) {
8104+
if (!isTypeAssignableTo(instantiateType(checkType, anyMapper), instantiateType(extendsType, constraintMapper))) {
81098105
return instantiateType(baseFalseType, mapper);
81108106
}
81118107
// Return a deferred type for a check that is neither definitely true nor definitely false
@@ -8704,8 +8700,8 @@ namespace ts {
87048700
function getConditionalTypeInstantiation(type: ConditionalType, mapper: TypeMapper): Type {
87058701
const target = type.target || type;
87068702
const combinedMapper = type.mapper ? combineTypeMappers(type.mapper, mapper) : mapper;
8707-
// Check if we have a conditional type of the form T extends U ? X : Y, where T is a type parameter.
8708-
// If so, the conditional type is distributive over a union type and when T is instantiated to a union
8703+
// Check if we have a conditional type where the check type is a naked type parameter. If so,
8704+
// the conditional type is distributive over union types and when T is instantiated to a union
87098705
// type A | B, we produce (A extends U ? X : Y) | (B extends U ? X : Y).
87108706
const checkType = target.checkType;
87118707
if (checkType.flags & TypeFlags.TypeParameter) {

0 commit comments

Comments
 (0)