From c8ac08549838c0dad0389e27ddf95bcc56760ad6 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Mon, 21 May 2018 15:28:51 -0700 Subject: [PATCH] Infer non-widening literal types when type parameter constraint includes primitive types --- src/compiler/checker.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 9f3c8fa6fd7..fb2e2ba3413 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -12777,7 +12777,9 @@ namespace ts { const widenLiteralTypes = inference.topLevel && !hasPrimitiveConstraint(inference.typeParameter) && (inference.isFixed || !isTypeParameterAtTopLevel(getReturnTypeOfSignature(signature), inference.typeParameter)); - const baseCandidates = widenLiteralTypes ? sameMap(candidates, getWidenedLiteralType) : candidates; + const baseCandidates = widenLiteralTypes ? sameMap(candidates, getWidenedLiteralType) : + hasPrimitiveConstraint(inference.typeParameter) ? sameMap(candidates, getRegularTypeOfLiteralType) : + candidates; // If all inferences were made from contravariant positions, infer a common subtype. Otherwise, if // union types were requested or if all inferences were made from the return type position, infer a // union type. Otherwise, infer a common supertype.