Skip to content

Commit 1b3589b

Browse files
committed
Remove simplification logic from getConditionalType + simplify substitution types
1 parent 4e040f7 commit 1b3589b

File tree

2 files changed

+11
-38
lines changed

2 files changed

+11
-38
lines changed

src/compiler/checker.ts

Lines changed: 10 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10196,6 +10196,7 @@ namespace ts {
1019610196
function getSimplifiedType(type: Type, writing: boolean): Type {
1019710197
return type.flags & TypeFlags.IndexedAccess ? getSimplifiedIndexedAccessType(<IndexedAccessType>type, writing) :
1019810198
type.flags & TypeFlags.Conditional ? getSimplifiedConditionalType(<ConditionalType>type, writing) :
10199+
type.flags & TypeFlags.Substitution ? writing ? (<SubstitutionType>type).typeVariable : (<SubstitutionType>type).substitute :
1019910200
type;
1020010201
}
1020110202

@@ -10260,10 +10261,10 @@ namespace ts {
1026010261
}
1026110262

1026210263
function getSimplifiedConditionalType(type: ConditionalType, writing: boolean) {
10263-
const falseType = getFalseTypeFromConditionalType(type);
10264-
const trueType = getTrueTypeFromConditionalType(type);
1026510264
const checkType = type.checkType;
1026610265
const extendsType = type.extendsType;
10266+
const trueType = getTrueTypeFromConditionalType(type);
10267+
const falseType = getFalseTypeFromConditionalType(type);
1026710268
// Simplifications for types of the form `T extends U ? T : never` and `T extends U ? never : T`.
1026810269
if (falseType.flags & TypeFlags.Never && getActualTypeVariable(trueType) === getActualTypeVariable(checkType)) {
1026910270
if (checkType.flags & TypeFlags.Any || isTypeAssignableTo(getRestrictiveInstantiation(checkType), getRestrictiveInstantiation(extendsType))) { // Always true
@@ -10281,10 +10282,16 @@ namespace ts {
1028110282
return getSimplifiedType(falseType, writing);
1028210283
}
1028310284
}
10284-
1028510285
return type;
1028610286
}
1028710287

10288+
/**
10289+
* Invokes union simplification logic to determine if an intersection is considered empty as a union constituent
10290+
*/
10291+
function isIntersectionEmpty(type1: Type, type2: Type) {
10292+
return !!(getUnionType([intersectTypes(type1, type2), neverType]).flags & TypeFlags.Never);
10293+
}
10294+
1028810295
function substituteIndexedMappedType(objectType: MappedType, index: Type) {
1028910296
const mapper = createTypeMapper([getTypeParameterFromMappedType(objectType)], [index]);
1029010297
const templateMapper = combineTypeMappers(objectType.mapper, mapper);
@@ -10391,36 +10398,12 @@ namespace ts {
1039110398
return type;
1039210399
}
1039310400

10394-
/**
10395-
* Invokes union simplification logic to determine if an intersection is considered empty as a union constituent
10396-
*/
10397-
function isIntersectionEmpty(type1: Type, type2: Type) {
10398-
return !!(getUnionType([intersectTypes(type1, type2), neverType]).flags & TypeFlags.Never);
10399-
}
10400-
1040110401
function getConditionalType(root: ConditionalRoot, mapper: TypeMapper | undefined): Type {
1040210402
const checkType = instantiateType(root.checkType, mapper);
1040310403
const extendsType = instantiateType(root.extendsType, mapper);
1040410404
if (checkType === wildcardType || extendsType === wildcardType) {
1040510405
return wildcardType;
1040610406
}
10407-
// Simplifications for types of the form `T extends U ? T : never` and `T extends U ? never : T`.
10408-
if (root.falseType.flags & TypeFlags.Never && getActualTypeVariable(root.trueType) === getActualTypeVariable(root.checkType)) {
10409-
if (checkType.flags & TypeFlags.Any || isTypeAssignableTo(getRestrictiveInstantiation(checkType), getRestrictiveInstantiation(extendsType))) { // Always true
10410-
return checkType;
10411-
}
10412-
else if (isIntersectionEmpty(checkType, extendsType)) { // Always false
10413-
return neverType;
10414-
}
10415-
}
10416-
else if (root.trueType.flags & TypeFlags.Never && getActualTypeVariable(root.falseType) === getActualTypeVariable(root.checkType)) {
10417-
if (!(checkType.flags & TypeFlags.Any) && isTypeAssignableTo(getRestrictiveInstantiation(checkType), getRestrictiveInstantiation(extendsType))) { // Always true
10418-
return neverType;
10419-
}
10420-
else if (checkType.flags & TypeFlags.Any || isIntersectionEmpty(checkType, extendsType)) { // Always false
10421-
return checkType;
10422-
}
10423-
}
1042410407
const checkTypeInstantiable = maybeTypeOfKind(checkType, TypeFlags.Instantiable | TypeFlags.GenericMappedType);
1042510408
let combinedMapper: TypeMapper | undefined;
1042610409
if (root.inferTypeParameters) {
@@ -10461,10 +10444,6 @@ namespace ts {
1046110444
}
1046210445
}
1046310446
// Return a deferred type for a check that is neither definitely true nor definitely false
10464-
return getDeferredConditionalType(root, mapper, combinedMapper, checkType, extendsType);
10465-
}
10466-
10467-
function getDeferredConditionalType(root: ConditionalRoot, mapper: TypeMapper | undefined, combinedMapper: TypeMapper | undefined, checkType: Type, extendsType: Type) {
1046810447
const erasedCheckType = getActualTypeVariable(checkType);
1046910448
const result = <ConditionalType>createType(TypeFlags.Conditional);
1047010449
result.root = root;
@@ -12453,12 +12432,6 @@ namespace ts {
1245312432
if (isFreshLiteralType(target)) {
1245412433
target = (<FreshableType>target).regularType;
1245512434
}
12456-
if (source.flags & TypeFlags.Substitution) {
12457-
source = (<SubstitutionType>source).substitute;
12458-
}
12459-
if (target.flags & TypeFlags.Substitution) {
12460-
target = (<SubstitutionType>target).typeVariable;
12461-
}
1246212435
if (source.flags & TypeFlags.Simplifiable) {
1246312436
source = getSimplifiedType(source, /*writing*/ false);
1246412437
}

src/compiler/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3967,7 +3967,7 @@ namespace ts {
39673967
/* @internal */
39683968
ObjectFlagsType = Nullable | Never | Object | Union | Intersection,
39693969
/* @internal */
3970-
Simplifiable = IndexedAccess | Conditional,
3970+
Simplifiable = IndexedAccess | Conditional | Substitution,
39713971
// 'Narrowable' types are types where narrowing actually narrows.
39723972
// This *should* be every type other than null, undefined, void, and never
39733973
Narrowable = Any | Unknown | StructuredOrInstantiable | StringLike | NumberLike | BigIntLike | BooleanLike | ESSymbol | UniqueESSymbol | NonPrimitive,

0 commit comments

Comments
 (0)