Skip to content

Commit 8528dbe

Browse files
committed
Handle union types in getRegularTypeOfLiteralType
1 parent c8ac085 commit 8528dbe

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

src/compiler/checker.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9286,8 +9286,10 @@ namespace ts {
92869286
return type;
92879287
}
92889288

9289-
function getRegularTypeOfLiteralType(type: Type) {
9290-
return type.flags & TypeFlags.StringOrNumberLiteral && type.flags & TypeFlags.FreshLiteral ? (<LiteralType>type).regularType : type;
9289+
function getRegularTypeOfLiteralType(type: Type): Type {
9290+
return type.flags & TypeFlags.StringOrNumberLiteral && type.flags & TypeFlags.FreshLiteral ? (<LiteralType>type).regularType :
9291+
type.flags & TypeFlags.Union ? getUnionType(sameMap((<UnionType>type).types, getRegularTypeOfLiteralType)) :
9292+
type;
92919293
}
92929294

92939295
function getLiteralType(value: string | number, enumId?: number, symbol?: Symbol) {
@@ -12774,11 +12776,11 @@ namespace ts {
1277412776
// all inferences were made to top-level occurrences of the type parameter, and
1277512777
// the type parameter has no constraint or its constraint includes no primitive or literal types, and
1277612778
// the type parameter was fixed during inference or does not occur at top-level in the return type.
12777-
const widenLiteralTypes = inference.topLevel &&
12778-
!hasPrimitiveConstraint(inference.typeParameter) &&
12779+
const primitiveConstraint = hasPrimitiveConstraint(inference.typeParameter);
12780+
const widenLiteralTypes = !primitiveConstraint && inference.topLevel &&
1277912781
(inference.isFixed || !isTypeParameterAtTopLevel(getReturnTypeOfSignature(signature), inference.typeParameter));
12780-
const baseCandidates = widenLiteralTypes ? sameMap(candidates, getWidenedLiteralType) :
12781-
hasPrimitiveConstraint(inference.typeParameter) ? sameMap(candidates, getRegularTypeOfLiteralType) :
12782+
const baseCandidates = primitiveConstraint ? sameMap(candidates, getRegularTypeOfLiteralType) :
12783+
widenLiteralTypes ? sameMap(candidates, getWidenedLiteralType) :
1278212784
candidates;
1278312785
// If all inferences were made from contravariant positions, infer a common subtype. Otherwise, if
1278412786
// union types were requested or if all inferences were made from the return type position, infer a

0 commit comments

Comments
 (0)