fix(48034): Get a literal string of type intersection in a template literal type (#48044)

This commit is contained in:
islandryu
2022-03-23 05:38:24 +09:00
committed by GitHub
parent 6cb58d382f
commit bf0eef4353
5 changed files with 218 additions and 5 deletions

View File

@@ -15256,24 +15256,32 @@ namespace ts {
}
return type;
function addSpans(texts: readonly string[], types: readonly Type[]): boolean {
function addSpans(texts: readonly string[] | string, types: readonly Type[]): boolean {
const isTextsArray = isArray(texts);
for (let i = 0; i < types.length; i++) {
const t = types[i];
const addText = isTextsArray ? texts[i + 1] : texts;
if (t.flags & (TypeFlags.Literal | TypeFlags.Null | TypeFlags.Undefined)) {
text += getTemplateStringForType(t) || "";
text += texts[i + 1];
text += addText;
if (!isTextsArray) return true;
}
else if (t.flags & TypeFlags.TemplateLiteral) {
text += (t as TemplateLiteralType).texts[0];
if (!addSpans((t as TemplateLiteralType).texts, (t as TemplateLiteralType).types)) return false;
text += texts[i + 1];
text += addText;
if (!isTextsArray) return true;
}
else if (isGenericIndexType(t) || isPatternLiteralPlaceholderType(t)) {
newTypes.push(t);
newTexts.push(text);
text = texts[i + 1];
text = addText;
}
else {
else if (t.flags & TypeFlags.Intersection) {
const added = addSpans(texts[i + 1], (t as IntersectionType).types);
if (!added) return false;
}
else if (isTextsArray) {
return false;
}
}