Skip to content

Commit 3811515

Browse files
committed
Fix subtype reduction involving type variables with union constraints
1 parent a70c409 commit 3811515

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

src/compiler/checker.ts

+10
Original file line numberDiff line numberDiff line change
@@ -16264,6 +16264,16 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
1626416264
i--;
1626516265
const source = types[i];
1626616266
if (hasEmptyObject || source.flags & TypeFlags.StructuredOrInstantiable) {
16267+
// A type parameter with a union constraint may be a subtype of some union, but not a subtype of the
16268+
// individual constituents of that union. For example, `T extends A | B` is a subtype of `A | B`, but not
16269+
// a subtype of just `A` or just `B`. When we encounter such a type parameter, we therefore check if the
16270+
// type parameter is a subtype of a union of all the other types.
16271+
if (source.flags & TypeFlags.TypeParameter && getBaseConstraintOrType(source).flags & TypeFlags.Union) {
16272+
if (isTypeRelatedTo(source, getUnionType(map(types, t => t === source ? neverType : t)), strictSubtypeRelation)) {
16273+
orderedRemoveItemAt(types, i);
16274+
}
16275+
continue;
16276+
}
1626716277
// Find the first property with a unit type, if any. When constituents have a property by the same name
1626816278
// but of a different unit type, we can quickly disqualify them from subtype checks. This helps subtype
1626916279
// reduction of large discriminated union types.

0 commit comments

Comments
 (0)