Skip to content

Commit 25a8ec3

Browse files
committed
Widen boolean literals when contextual type is full boolean type
1 parent 073ac92 commit 25a8ec3

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/compiler/checker.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34206,8 +34206,8 @@ namespace ts {
3420634206
function isLiteralOfContextualType(candidateType: Type, contextualType: Type | undefined): boolean {
3420734207
if (contextualType) {
3420834208
if (contextualType.flags & TypeFlags.UnionOrIntersection) {
34209-
const types = (contextualType as UnionType).types;
34210-
return some(types, t => isLiteralOfContextualType(candidateType, t));
34209+
const skipBooleanLiterals = containsBooleanType(contextualType);
34210+
return some((contextualType as UnionType).types, t => !(skipBooleanLiterals && t.flags & TypeFlags.BooleanLiteral) && isLiteralOfContextualType(candidateType, t));
3421134211
}
3421234212
if (contextualType.flags & TypeFlags.InstantiableNonPrimitive) {
3421334213
// If the contextual type is a type variable constrained to a primitive type, consider
@@ -34217,6 +34217,7 @@ namespace ts {
3421734217
return maybeTypeOfKind(constraint, TypeFlags.String) && maybeTypeOfKind(candidateType, TypeFlags.StringLiteral) ||
3421834218
maybeTypeOfKind(constraint, TypeFlags.Number) && maybeTypeOfKind(candidateType, TypeFlags.NumberLiteral) ||
3421934219
maybeTypeOfKind(constraint, TypeFlags.BigInt) && maybeTypeOfKind(candidateType, TypeFlags.BigIntLiteral) ||
34220+
containsBooleanType(constraint) && maybeTypeOfKind(candidateType, TypeFlags.BooleanLiteral) ||
3422034221
maybeTypeOfKind(constraint, TypeFlags.ESSymbol) && maybeTypeOfKind(candidateType, TypeFlags.UniqueESSymbol) ||
3422134222
isLiteralOfContextualType(candidateType, constraint);
3422234223
}
@@ -34231,6 +34232,10 @@ namespace ts {
3423134232
return false;
3423234233
}
3423334234

34235+
function containsBooleanType(type: Type) {
34236+
return !!(type.flags & TypeFlags.Union) && containsType((type as UnionType).types, regularFalseType) && containsType((type as UnionType).types, regularTrueType);
34237+
}
34238+
3423434239
function isConstContext(node: Expression): boolean {
3423534240
const parent = node.parent;
3423634241
return isAssertionExpression(parent) && isConstTypeReference(parent.type) ||

0 commit comments

Comments
 (0)