From 614d171a21b5c9e95e60a6c62ecec3e97879de6f Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Sat, 16 Jul 2016 14:45:38 -0700 Subject: [PATCH] Include type parameter constrains in literal type context determination --- src/compiler/checker.ts | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 47fa9918d2b..bbe74231f5a 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -13007,8 +13007,22 @@ namespace ts { false; } + function hasLiteralContextualType(node: Expression) { + const contextualType = getContextualType(node); + if (!contextualType) { + return false; + } + if (contextualType.flags & TypeFlags.TypeParameter) { + const apparentType = getApparentTypeOfTypeParameter(contextualType); + if (apparentType.flags & (TypeFlags.StringLike | TypeFlags.NumberLike | TypeFlags.BooleanLike)) { + return true; + } + } + return isLiteralUnionType(contextualType); + } + function isLiteralTypeContext(node: Expression) { - return isLiteralTypeLocation(node) || isLiteralUnionType(getContextualType(node) || unknownType); + return isLiteralTypeLocation(node) || hasLiteralContextualType(node); } function checkLiteralExpression(node: Expression): Type {