mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-11-18 17:21:48 +00:00
Always use a string literal type if contextually typed by any string literal types.
This commit is contained in:
+13
-13
@@ -5691,6 +5691,10 @@ namespace ts {
|
||||
return !!getPropertyOfType(type, "0");
|
||||
}
|
||||
|
||||
function isStringLiteralType(type: Type) {
|
||||
return type.flags & TypeFlags.StringLiteral;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a Type was written as a tuple type literal.
|
||||
* Prefer using isTupleLikeType() unless the use of `elementTypes` is required.
|
||||
@@ -6953,6 +6957,10 @@ namespace ts {
|
||||
return applyToContextualType(type, t => getIndexTypeOfStructuredType(t, kind));
|
||||
}
|
||||
|
||||
function contextualTypeIsStringLiteralType(type: Type): boolean {
|
||||
return !!(type.flags & TypeFlags.Union ? forEach((<UnionType>type).types, isStringLiteralType) : isStringLiteralType(type));
|
||||
}
|
||||
|
||||
// Return true if the given contextual type is a tuple-like type
|
||||
function contextualTypeIsTupleLikeType(type: Type): boolean {
|
||||
return !!(type.flags & TypeFlags.Union ? forEach((<UnionType>type).types, isTupleLikeType) : isTupleLikeType(type));
|
||||
@@ -10350,22 +10358,14 @@ namespace ts {
|
||||
return getUnionType([type1, type2]);
|
||||
}
|
||||
|
||||
function checkStringLiteralExpression(node: LiteralExpression) {
|
||||
function checkStringLiteralExpression(node: StringLiteral): Type {
|
||||
// TODO (drosen): Do we want to apply the same approach to no-sub template literals?
|
||||
|
||||
let contextualType = getContextualType(node);
|
||||
if (contextualType) {
|
||||
if (contextualType.flags & TypeFlags.Union) {
|
||||
for (const type of (<UnionType>contextualType).types) {
|
||||
if (type.flags & TypeFlags.StringLiteral && (<StringLiteralType>type).text === node.text) {
|
||||
return type;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (contextualType.flags & TypeFlags.StringLiteral && (<StringLiteralType>contextualType).text === node.text) {
|
||||
return contextualType;
|
||||
}
|
||||
if (contextualType && contextualTypeIsStringLiteralType(contextualType)) {
|
||||
return getStringLiteralType(node);
|
||||
}
|
||||
|
||||
return stringType;
|
||||
}
|
||||
|
||||
@@ -10499,7 +10499,7 @@ namespace ts {
|
||||
case SyntaxKind.TemplateExpression:
|
||||
return checkTemplateExpression(<TemplateExpression>node);
|
||||
case SyntaxKind.StringLiteral:
|
||||
return checkStringLiteralExpression(<LiteralExpression>node);
|
||||
return checkStringLiteralExpression(<StringLiteral>node);
|
||||
case SyntaxKind.NoSubstitutionTemplateLiteral:
|
||||
return stringType;
|
||||
case SyntaxKind.RegularExpressionLiteral:
|
||||
|
||||
Reference in New Issue
Block a user