Skip to content

Commit 9ea685b

Browse files
committed
Simplify relationship check for conditional type on target side
1 parent 8d715ff commit 9ea685b

File tree

1 file changed

+7
-34
lines changed

1 file changed

+7
-34
lines changed

src/compiler/checker.ts

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -15315,13 +15315,6 @@ namespace ts {
1531515315
return type[cache] = type;
1531615316
}
1531715317

15318-
function isConditionalTypeAlwaysTrueDisregardingInferTypes(type: ConditionalType) {
15319-
const extendsInferParamMapper = type.root.inferTypeParameters && createTypeMapper(type.root.inferTypeParameters, map(type.root.inferTypeParameters, () => wildcardType));
15320-
const checkType = type.checkType;
15321-
const extendsType = type.extendsType;
15322-
return isTypeAssignableTo(getRestrictiveInstantiation(checkType), getRestrictiveInstantiation(instantiateType(extendsType, extendsInferParamMapper)));
15323-
}
15324-
1532515318
function getSimplifiedConditionalType(type: ConditionalType, writing: boolean) {
1532615319
const checkType = type.checkType;
1532715320
const extendsType = type.extendsType;
@@ -19030,35 +19023,15 @@ namespace ts {
1903019023
resetErrorInfo(saveErrorInfo);
1903119024
return Ternary.Maybe;
1903219025
}
19033-
const c = target as ConditionalType;
19034-
// Check if the conditional is always true or always false but still deferred for distribution purposes
19035-
const skipTrue = !isTypeAssignableTo(getPermissiveInstantiation(c.checkType), getPermissiveInstantiation(c.extendsType));
19036-
const skipFalse = !skipTrue && isConditionalTypeAlwaysTrueDisregardingInferTypes(c);
19037-
19038-
// Instantiate with a replacement mapper if the conditional is distributive, replacing the check type with a clone of itself,
19039-
// this way {x: string | number, y: string | number} -> (T extends T ? { x: T, y: T } : never) appropriately _fails_ when
19040-
// T = string | number (since that will end up distributing and producing `{x: string, y: string} | {x: number, y: number}`,
19041-
// to which `{x: string | number, y: string | number}` isn't assignable)
19042-
let distributionMapper: TypeMapper | undefined;
19043-
const checkVar = getActualTypeVariable(c.root.checkType);
19044-
if (c.root.isDistributive && checkVar.flags & TypeFlags.TypeParameter) {
19045-
const newParam = cloneTypeParameter(checkVar);
19046-
distributionMapper = prependTypeMapping(checkVar, newParam, c.mapper);
19047-
newParam.mapper = distributionMapper;
19048-
}
19049-
19050-
// TODO: Find a nice way to include potential conditional type breakdowns in error output, if they seem good (they usually don't)
19051-
const expanding = isDeeplyNestedType(target, targetStack, targetDepth);
19052-
let localResult: Ternary | undefined = expanding ? Ternary.Maybe : undefined;
19053-
if (skipTrue || expanding || (localResult = isRelatedTo(source, distributionMapper ? instantiateType(getTypeFromTypeNode(c.root.node.trueType), distributionMapper) : getTrueTypeFromConditionalType(c), RecursionFlags.Target, /*reportErrors*/ false))) {
19054-
if (!skipFalse && !expanding) {
19055-
localResult = (localResult || Ternary.Maybe) & isRelatedTo(source, distributionMapper ? instantiateType(getTypeFromTypeNode(c.root.node.falseType), distributionMapper) : getFalseTypeFromConditionalType(c), RecursionFlags.Target, /*reportErrors*/ false);
19026+
if (!(target as ConditionalType).root.inferTypeParameters) {
19027+
if (result = isRelatedTo(source, getTrueTypeFromConditionalType(target as ConditionalType), RecursionFlags.Target, /*reportErrors*/ false)) {
19028+
result &= isRelatedTo(source, getFalseTypeFromConditionalType(target as ConditionalType), RecursionFlags.Target, /*reportErrors*/ false);
19029+
if (result) {
19030+
resetErrorInfo(saveErrorInfo);
19031+
return result;
19032+
}
1905619033
}
1905719034
}
19058-
if (localResult) {
19059-
resetErrorInfo(saveErrorInfo);
19060-
return localResult;
19061-
}
1906219035
}
1906319036
else if (target.flags & TypeFlags.TemplateLiteral) {
1906419037
if (source.flags & TypeFlags.TemplateLiteral) {

0 commit comments

Comments
 (0)