Skip to content

Commit 0ccbad0

Browse files
ahejlsbergJack-Works
authored andcommitted
Fewer array copy operations in template literal type resolution
1 parent bf6c809 commit 0ccbad0

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

src/compiler/checker.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13435,32 +13435,38 @@ namespace ts {
1343513435
mapType(types[unionIndex], t => getTemplateType(texts, casings, replaceElement(types, unionIndex, t))) :
1343613436
errorType;
1343713437
}
13438-
let i = 0;
13439-
while (i < types.length) {
13438+
let newTypes = [];
13439+
let newCasings = [];
13440+
let newTexts = [];
13441+
let text = texts[0];
13442+
for (let i = 0; i < types.length; i++) {
1344013443
const t = types[i];
1344113444
const casingType = casings[i];
1344213445
const isGeneric = isGenericIndexType(t);
1344313446
const resolvable = (t.flags & TypeFlags.Literal) || (!isGeneric && casingType === TemplateCasing.TypeOf);
1344413447
if (resolvable) {
1344513448
const s = applyTemplateCasing(getTemplateStringForType(t, casingType) || "", casingType);
13446-
texts = [...texts.slice(0, i), texts[i] + s + texts[i + 1], ...texts.slice(i + 2)];
13447-
casings = [...casings.slice(0, i), ...casings.slice(i + 1)];
13448-
types = [...types.slice(0, i), ...types.slice(i + 1)];
13449+
text += s;
13450+
text += texts[i + 1];
1344913451
}
1345013452
else if (isGeneric) {
13451-
i++;
13453+
newTypes.push(t);
13454+
newCasings.push(casings[i]);
13455+
newTexts.push(text);
13456+
text = texts[i + 1];
1345213457
}
1345313458
else {
1345413459
return stringType;
1345513460
}
1345613461
}
13457-
if (types.length === 0) {
13458-
return getLiteralType(texts[0]);
13462+
if (newTypes.length === 0) {
13463+
return getLiteralType(text);
1345913464
}
13460-
const id = `${getTypeListId(types)}|${casings.join(",")}|${map(texts, t => t.length).join(",")}|${texts.join("")}`;
13465+
newTexts.push(text);
13466+
const id = `${getTypeListId(newTypes)}|${newCasings.join(",")}|${map(newTexts, t => t.length).join(",")}|${newTexts.join("")}`;
1346113467
let type = templateTypes.get(id);
1346213468
if (!type) {
13463-
templateTypes.set(id, type = createTemplateType(texts, casings, types));
13469+
templateTypes.set(id, type = createTemplateType(newTexts, newCasings, newTypes));
1346413470
}
1346513471
return type;
1346613472
}

0 commit comments

Comments
 (0)