@@ -16548,7 +16548,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
16548
16548
for (const u of unionTypes) {
16549
16549
if (!containsType(u.types, type)) {
16550
16550
const primitive = type.flags & TypeFlags.StringLiteral ? stringType :
16551
- type.flags & TypeFlags.NumberLiteral ? numberType :
16551
+ type.flags & ( TypeFlags.Enum | TypeFlags. NumberLiteral) ? numberType :
16552
16552
type.flags & TypeFlags.BigIntLiteral ? bigintType :
16553
16553
type.flags & TypeFlags.UniqueESSymbol ? esSymbolType :
16554
16554
undefined;
@@ -16584,10 +16584,6 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
16584
16584
return false;
16585
16585
}
16586
16586
16587
- function eachIsUnionContaining(types: Type[], flag: TypeFlags) {
16588
- return every(types, t => !!(t.flags & TypeFlags.Union) && some((t as UnionType).types, tt => !!(tt.flags & flag)));
16589
- }
16590
-
16591
16587
function removeFromEach(types: Type[], flag: TypeFlags) {
16592
16588
for (let i = 0; i < types.length; i++) {
16593
16589
types[i] = filterType(types[i], t => !(t.flags & flag));
@@ -16719,12 +16715,12 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
16719
16715
// reduced we'll never reduce again, so this occurs at most once.
16720
16716
result = getIntersectionType(typeSet, aliasSymbol, aliasTypeArguments);
16721
16717
}
16722
- else if (eachIsUnionContaining (typeSet, TypeFlags.Undefined)) {
16718
+ else if (every (typeSet, t => !!(t.flags & TypeFlags.Union && (t as UnionType).types[0].flags & TypeFlags. Undefined) )) {
16723
16719
const containedUndefinedType = some(typeSet, containsMissingType) ? missingType : undefinedType;
16724
16720
removeFromEach(typeSet, TypeFlags.Undefined);
16725
16721
result = getUnionType([getIntersectionType(typeSet), containedUndefinedType], UnionReduction.Literal, aliasSymbol, aliasTypeArguments);
16726
16722
}
16727
- else if (eachIsUnionContaining (typeSet, TypeFlags.Null)) {
16723
+ else if (every (typeSet, t => !!(t.flags & TypeFlags.Union && ((t as UnionType).types[0].flags & TypeFlags. Null || (t as UnionType).types[1].flags & TypeFlags.Null)) )) {
16728
16724
removeFromEach(typeSet, TypeFlags.Null);
16729
16725
result = getUnionType([getIntersectionType(typeSet), nullType], UnionReduction.Literal, aliasSymbol, aliasTypeArguments);
16730
16726
}
@@ -20837,6 +20833,20 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
20837
20833
if (containsType(targetTypes, source)) {
20838
20834
return Ternary.True;
20839
20835
}
20836
+ if (getObjectFlags(target) & ObjectFlags.PrimitiveUnion && !(source.flags & TypeFlags.EnumLiteral) && (
20837
+ source.flags & (TypeFlags.StringLiteral | TypeFlags.BooleanLiteral | TypeFlags.BigIntLiteral) ||
20838
+ (relation === subtypeRelation || relation === strictSubtypeRelation) && source.flags & TypeFlags.NumberLiteral)) {
20839
+ // When relating a literal type to a union of primitive types, we know the relation is false unless
20840
+ // the union contains the base primitive type or the string literal type in one of its fresh/regular forms.
20841
+ // We exclude numeric literals for non-subtype relations because numeric literals are assignable to
20842
+ // numeric enum literals with the same value.
20843
+ const alternateForm = source === (source as StringLiteralType).regularType ? (source as StringLiteralType).freshType : (source as StringLiteralType).regularType;
20844
+ const primitive = source.flags & TypeFlags.StringLiteral ? stringType :
20845
+ source.flags & TypeFlags.NumberLiteral ? numberType :
20846
+ source.flags & TypeFlags.BigIntLiteral ? bigintType :
20847
+ undefined;
20848
+ return primitive && containsType(targetTypes, primitive) || alternateForm && containsType(targetTypes, alternateForm) ? Ternary.True : Ternary.False;
20849
+ }
20840
20850
const match = getMatchingUnionConstituentForType(target as UnionType, source);
20841
20851
if (match) {
20842
20852
const related = isRelatedTo(source, match, RecursionFlags.Target, /*reportErrors*/ false);
0 commit comments