Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify relationship check for conditional type on target side #46429

Merged
merged 5 commits into from
Oct 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 21 additions & 35 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15315,13 +15315,6 @@ namespace ts {
return type[cache] = type;
}

function isConditionalTypeAlwaysTrueDisregardingInferTypes(type: ConditionalType) {
const extendsInferParamMapper = type.root.inferTypeParameters && createTypeMapper(type.root.inferTypeParameters, map(type.root.inferTypeParameters, () => wildcardType));
const checkType = type.checkType;
const extendsType = type.extendsType;
return isTypeAssignableTo(getRestrictiveInstantiation(checkType), getRestrictiveInstantiation(instantiateType(extendsType, extendsInferParamMapper)));
}

function getSimplifiedConditionalType(type: ConditionalType, writing: boolean) {
const checkType = type.checkType;
const extendsType = type.extendsType;
Expand Down Expand Up @@ -15649,6 +15642,10 @@ namespace ts {
const links = getNodeLinks(node);
if (!links.resolvedType) {
const checkType = getTypeFromTypeNode(node.checkType);
const isDistributive = !!(checkType.flags & TypeFlags.TypeParameter);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this code was already put in place - but don't we distribute on indexed access types or something like that as well?

const isDistributionDependent = isDistributive && (
isTypeParameterPossiblyReferenced(checkType as TypeParameter, node.trueType) ||
isTypeParameterPossiblyReferenced(checkType as TypeParameter, node.falseType));
const aliasSymbol = getAliasSymbolForTypeNode(node);
const aliasTypeArguments = getTypeArgumentsForAliasSymbol(aliasSymbol);
const allOuterTypeParameters = getOuterTypeParameters(node, /*includeThisTypes*/ true);
Expand All @@ -15657,7 +15654,8 @@ namespace ts {
node,
checkType,
extendsType: getTypeFromTypeNode(node.extendsType),
isDistributive: !!(checkType.flags & TypeFlags.TypeParameter),
isDistributive,
isDistributionDependent,
inferTypeParameters: getInferTypeParameters(node),
outerTypeParameters,
instantiations: undefined,
Expand Down Expand Up @@ -19031,33 +19029,21 @@ namespace ts {
return Ternary.Maybe;
}
const c = target as ConditionalType;
// Check if the conditional is always true or always false but still deferred for distribution purposes
const skipTrue = !isTypeAssignableTo(getPermissiveInstantiation(c.checkType), getPermissiveInstantiation(c.extendsType));
const skipFalse = !skipTrue && isConditionalTypeAlwaysTrueDisregardingInferTypes(c);

// Instantiate with a replacement mapper if the conditional is distributive, replacing the check type with a clone of itself,
// this way {x: string | number, y: string | number} -> (T extends T ? { x: T, y: T } : never) appropriately _fails_ when
// T = string | number (since that will end up distributing and producing `{x: string, y: string} | {x: number, y: number}`,
// to which `{x: string | number, y: string | number}` isn't assignable)
let distributionMapper: TypeMapper | undefined;
const checkVar = getActualTypeVariable(c.root.checkType);
if (c.root.isDistributive && checkVar.flags & TypeFlags.TypeParameter) {
const newParam = cloneTypeParameter(checkVar);
distributionMapper = prependTypeMapping(checkVar, newParam, c.mapper);
newParam.mapper = distributionMapper;
}

// TODO: Find a nice way to include potential conditional type breakdowns in error output, if they seem good (they usually don't)
const expanding = isDeeplyNestedType(target, targetStack, targetDepth);
let localResult: Ternary | undefined = expanding ? Ternary.Maybe : undefined;
if (skipTrue || expanding || (localResult = isRelatedTo(source, distributionMapper ? instantiateType(getTypeFromTypeNode(c.root.node.trueType), distributionMapper) : getTrueTypeFromConditionalType(c), RecursionFlags.Target, /*reportErrors*/ false))) {
if (!skipFalse && !expanding) {
localResult = (localResult || Ternary.Maybe) & isRelatedTo(source, distributionMapper ? instantiateType(getTypeFromTypeNode(c.root.node.falseType), distributionMapper) : getFalseTypeFromConditionalType(c), RecursionFlags.Target, /*reportErrors*/ false);
}
}
if (localResult) {
resetErrorInfo(saveErrorInfo);
return localResult;
// We check for a relationship to a conditional type target only when the conditional type has no
// 'infer' positions and is not distributive or is distributive but doesn't reference the check type
// parameter in either of the result types.
if (!c.root.inferTypeParameters && !c.root.isDistributionDependent) {
// Check if the conditional is always true or always false but still deferred for distribution purposes.
const skipTrue = !isTypeAssignableTo(getPermissiveInstantiation(c.checkType), getPermissiveInstantiation(c.extendsType));
const skipFalse = !skipTrue && isTypeAssignableTo(getRestrictiveInstantiation(c.checkType), getRestrictiveInstantiation(c.extendsType));
// TODO: Find a nice way to include potential conditional type breakdowns in error output, if they seem good (they usually don't)
if (result = skipTrue ? Ternary.True : isRelatedTo(source, getTrueTypeFromConditionalType(c), RecursionFlags.Target, /*reportErrors*/ false)) {
result &= skipFalse ? Ternary.True : isRelatedTo(source, getFalseTypeFromConditionalType(c), RecursionFlags.Target, /*reportErrors*/ false);
if (result) {
resetErrorInfo(saveErrorInfo);
return result;
}
}
}
}
else if (target.flags & TypeFlags.TemplateLiteral) {
Expand Down
1 change: 1 addition & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5648,6 +5648,7 @@ namespace ts {
checkType: Type;
extendsType: Type;
isDistributive: boolean;
isDistributionDependent: boolean;
inferTypeParameters?: TypeParameter[];
outerTypeParameters?: TypeParameter[];
instantiations?: Map<Type>;
Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/api/tsserverlibrary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2747,6 +2747,7 @@ declare namespace ts {
checkType: Type;
extendsType: Type;
isDistributive: boolean;
isDistributionDependent: boolean;
inferTypeParameters?: TypeParameter[];
outerTypeParameters?: TypeParameter[];
instantiations?: Map<Type>;
Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2747,6 +2747,7 @@ declare namespace ts {
checkType: Type;
extendsType: Type;
isDistributive: boolean;
isDistributionDependent: boolean;
inferTypeParameters?: TypeParameter[];
outerTypeParameters?: TypeParameter[];
instantiations?: Map<Type>;
Expand Down