Skip to content

Commit 25af351

Browse files
committed
Nix getBestChoiceType, [] subtyping, nominal union reduction for classes
1 parent 923e7a0 commit 25af351

File tree

1 file changed

+10
-14
lines changed

1 file changed

+10
-14
lines changed

src/compiler/checker.ts

+10-14
Original file line numberDiff line numberDiff line change
@@ -7434,8 +7434,8 @@ namespace ts {
74347434
function isSubtypeOfAny(source: Type, targets: Type[]): boolean {
74357435
for (const target of targets) {
74367436
if (source !== target && isTypeSubtypeOf(source, target) && (
7437-
!(getObjectFlags(source) & (ObjectFlags.ClassOrInterface | ObjectFlags.Reference)) ||
7438-
!(getObjectFlags(target) & (ObjectFlags.ClassOrInterface | ObjectFlags.Reference)) ||
7437+
!(getObjectFlags(getTargetType(source)) & ObjectFlags.Class) ||
7438+
!(getObjectFlags(getTargetType(target)) & ObjectFlags.Class) ||
74397439
isTypeDerivedFrom(source, target))) {
74407440
return true;
74417441
}
@@ -9605,7 +9605,7 @@ namespace ts {
96059605
if (relation === identityRelation) {
96069606
return propertiesIdenticalTo(source, target);
96079607
}
9608-
const requireOptionalProperties = relation === subtypeRelation && !isObjectLiteralType(source);
9608+
const requireOptionalProperties = relation === subtypeRelation && !isObjectLiteralType(source) && !isEmptyArrayLiteralType(source);
96099609
const unmatchedProperty = getUnmatchedProperty(source, target, requireOptionalProperties);
96109610
if (unmatchedProperty) {
96119611
if (reportErrors) {
@@ -10313,6 +10313,11 @@ namespace ts {
1031310313
!(type.flags & TypeFlags.Nullable) && isTypeAssignableTo(type, anyReadonlyArrayType);
1031410314
}
1031510315

10316+
function isEmptyArrayLiteralType(type: Type): boolean {
10317+
const elementType = isArrayType(type) ? (<TypeReference>type).typeArguments[0] : undefined;
10318+
return elementType === undefinedWideningType || elementType === neverType;
10319+
}
10320+
1031610321
function isTupleLikeType(type: Type): boolean {
1031710322
return !!getPropertyOfType(type, "0" as __String);
1031810323
}
@@ -13878,7 +13883,6 @@ namespace ts {
1387813883
type.pattern = node;
1387913884
return type;
1388013885
}
13881-
const contextualType = getApparentTypeOfContextualType(node);
1388213886
if (contextualType && contextualTypeIsTupleLikeType(contextualType)) {
1388313887
const pattern = contextualType.pattern;
1388413888
// If array literal is contextually typed by a binding pattern or an assignment pattern, pad the resulting
@@ -18066,14 +18070,6 @@ namespace ts {
1806618070
return (target.flags & TypeFlags.Nullable) !== 0 || isTypeComparableTo(source, target);
1806718071
}
1806818072

18069-
function getBestChoiceType(type1: Type, type2: Type): Type {
18070-
const firstAssignableToSecond = isTypeAssignableTo(type1, type2);
18071-
const secondAssignableToFirst = isTypeAssignableTo(type2, type1);
18072-
return secondAssignableToFirst && !firstAssignableToSecond ? type1 :
18073-
firstAssignableToSecond && !secondAssignableToFirst ? type2 :
18074-
getUnionType([type1, type2], /*subtypeReduction*/ true);
18075-
}
18076-
1807718073
function checkBinaryExpression(node: BinaryExpression, checkMode?: CheckMode) {
1807818074
return checkBinaryLikeExpression(node.left, node.operatorToken, node.right, checkMode, node);
1807918075
}
@@ -18210,7 +18206,7 @@ namespace ts {
1821018206
leftType;
1821118207
case SyntaxKind.BarBarToken:
1821218208
return getTypeFacts(leftType) & TypeFacts.Falsy ?
18213-
getBestChoiceType(removeDefinitelyFalsyTypes(leftType), rightType) :
18209+
getUnionType([removeDefinitelyFalsyTypes(leftType), rightType], /*subtypeReduction*/ true) :
1821418210
leftType;
1821518211
case SyntaxKind.EqualsToken:
1821618212
checkAssignmentOperator(rightType);
@@ -18370,7 +18366,7 @@ namespace ts {
1837018366
checkExpression(node.condition);
1837118367
const type1 = checkExpression(node.whenTrue, checkMode);
1837218368
const type2 = checkExpression(node.whenFalse, checkMode);
18373-
return getBestChoiceType(type1, type2);
18369+
return getUnionType([type1, type2], /*subtypeReduction*/ true);
1837418370
}
1837518371

1837618372
function checkTemplateExpression(node: TemplateExpression): Type {

0 commit comments

Comments
 (0)