Skip to content

Commit c266e47

Browse files
authored
Fix support for intersections in template literal placeholder types (#56434)
1 parent 38ef79e commit c266e47

8 files changed

+101
-49
lines changed

src/compiler/checker.ts

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16925,10 +16925,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
1692516925
}
1692616926

1692716927
function removeStringLiteralsMatchedByTemplateLiterals(types: Type[]) {
16928-
const templates = filter(types, t =>
16929-
!!(t.flags & TypeFlags.TemplateLiteral) &&
16930-
isPatternLiteralType(t) &&
16931-
(t as TemplateLiteralType).types.every(t => !(t.flags & TypeFlags.Intersection) || !areIntersectedTypesAvoidingPrimitiveReduction((t as IntersectionType).types))) as TemplateLiteralType[];
16928+
const templates = filter(types, t => !!(t.flags & TypeFlags.TemplateLiteral) && isPatternLiteralType(t)) as TemplateLiteralType[];
1693216929
if (templates.length) {
1693316930
let i = types.length;
1693416931
while (i > 0) {
@@ -17439,20 +17436,17 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
1743917436
return reduceLeft(types, (n, t) => n + getConstituentCount(t), 0);
1744017437
}
1744117438

17442-
function areIntersectedTypesAvoidingPrimitiveReduction(types: Type[], primitiveFlags = TypeFlags.String | TypeFlags.Number | TypeFlags.BigInt): boolean {
17443-
if (types.length !== 2) {
17444-
return false;
17445-
}
17446-
const [t1, t2] = types;
17447-
return !!(t1.flags & primitiveFlags) && t2 === emptyTypeLiteralType || !!(t2.flags & primitiveFlags) && t1 === emptyTypeLiteralType;
17448-
}
17449-
1745017439
function getTypeFromIntersectionTypeNode(node: IntersectionTypeNode): Type {
1745117440
const links = getNodeLinks(node);
1745217441
if (!links.resolvedType) {
1745317442
const aliasSymbol = getAliasSymbolForTypeNode(node);
1745417443
const types = map(node.types, getTypeFromTypeNode);
17455-
const noSupertypeReduction = areIntersectedTypesAvoidingPrimitiveReduction(types);
17444+
// We perform no supertype reduction for X & {} or {} & X, where X is one of string, number, bigint,
17445+
// or a pattern literal template type. This enables union types like "a" | "b" | string & {} or
17446+
// "aa" | "ab" | `a${string}` which preserve the literal types for purposes of statement completion.
17447+
const emptyIndex = types.length === 2 ? types.indexOf(emptyTypeLiteralType) : -1;
17448+
const t = emptyIndex >= 0 ? types[1 - emptyIndex] : unknownType;
17449+
const noSupertypeReduction = !!(t.flags & (TypeFlags.String | TypeFlags.Number | TypeFlags.BigInt) || t.flags & TypeFlags.TemplateLiteral && isPatternLiteralType(t));
1745617450
links.resolvedType = getIntersectionType(types, aliasSymbol, getTypeArgumentsForAliasSymbol(aliasSymbol), noSupertypeReduction);
1745717451
}
1745817452
return links.resolvedType;
@@ -17732,7 +17726,6 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
1773217726

1773317727
function createTemplateLiteralType(texts: readonly string[], types: readonly Type[]) {
1773417728
const type = createType(TypeFlags.TemplateLiteral) as TemplateLiteralType;
17735-
type.objectFlags = getPropagatingFlagsOfTypes(types, /*excludeKinds*/ TypeFlags.Nullable);
1773617729
type.texts = texts;
1773717730
type.types = types;
1773817731
return type;
@@ -18057,12 +18050,25 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
1805718050

1805818051
function isPatternLiteralPlaceholderType(type: Type): boolean {
1805918052
if (type.flags & TypeFlags.Intersection) {
18060-
return !isGenericType(type) && some((type as IntersectionType).types, t => !!(t.flags & (TypeFlags.Literal | TypeFlags.Nullable)) || isPatternLiteralPlaceholderType(t));
18053+
// Return true if the intersection consists of one or more placeholders and zero or
18054+
// more object type tags.
18055+
let seenPlaceholder = false;
18056+
for (const t of (type as IntersectionType).types) {
18057+
if (t.flags & (TypeFlags.Literal | TypeFlags.Nullable) || isPatternLiteralPlaceholderType(t)) {
18058+
seenPlaceholder = true;
18059+
}
18060+
else if (!(t.flags & TypeFlags.Object)) {
18061+
return false;
18062+
}
18063+
}
18064+
return seenPlaceholder;
1806118065
}
1806218066
return !!(type.flags & (TypeFlags.Any | TypeFlags.String | TypeFlags.Number | TypeFlags.BigInt)) || isPatternLiteralType(type);
1806318067
}
1806418068

1806518069
function isPatternLiteralType(type: Type) {
18070+
// A pattern literal type is a template literal or a string mapping type that contains only
18071+
// non-generic pattern literal placeholders.
1806618072
return !!(type.flags & TypeFlags.TemplateLiteral) && every((type as TemplateLiteralType).types, isPatternLiteralPlaceholderType) ||
1806718073
!!(type.flags & TypeFlags.StringMapping) && isPatternLiteralPlaceholderType((type as StringMappingType).type);
1806818074
}
@@ -18080,12 +18086,12 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
1808018086
}
1808118087

1808218088
function getGenericObjectFlags(type: Type): ObjectFlags {
18083-
if (type.flags & (TypeFlags.UnionOrIntersection | TypeFlags.TemplateLiteral)) {
18084-
if (!((type as UnionOrIntersectionType | TemplateLiteralType).objectFlags & ObjectFlags.IsGenericTypeComputed)) {
18085-
(type as UnionOrIntersectionType | TemplateLiteralType).objectFlags |= ObjectFlags.IsGenericTypeComputed |
18086-
reduceLeft((type as UnionOrIntersectionType | TemplateLiteralType).types, (flags, t) => flags | getGenericObjectFlags(t), 0);
18089+
if (type.flags & (TypeFlags.UnionOrIntersection)) {
18090+
if (!((type as UnionOrIntersectionType).objectFlags & ObjectFlags.IsGenericTypeComputed)) {
18091+
(type as UnionOrIntersectionType).objectFlags |= ObjectFlags.IsGenericTypeComputed |
18092+
reduceLeft((type as UnionOrIntersectionType).types, (flags, t) => flags | getGenericObjectFlags(t), 0);
1808718093
}
18088-
return (type as UnionOrIntersectionType | TemplateLiteralType).objectFlags & ObjectFlags.IsGenericType;
18094+
return (type as UnionOrIntersectionType).objectFlags & ObjectFlags.IsGenericType;
1808918095
}
1809018096
if (type.flags & TypeFlags.Substitution) {
1809118097
if (!((type as SubstitutionType).objectFlags & ObjectFlags.IsGenericTypeComputed)) {
@@ -18095,7 +18101,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
1809518101
return (type as SubstitutionType).objectFlags & ObjectFlags.IsGenericType;
1809618102
}
1809718103
return (type.flags & TypeFlags.InstantiableNonPrimitive || isGenericMappedType(type) || isGenericTupleType(type) ? ObjectFlags.IsGenericObjectType : 0) |
18098-
(type.flags & (TypeFlags.InstantiableNonPrimitive | TypeFlags.Index | TypeFlags.StringMapping) && !isPatternLiteralType(type) ? ObjectFlags.IsGenericIndexType : 0);
18104+
(type.flags & (TypeFlags.InstantiableNonPrimitive | TypeFlags.Index | TypeFlags.TemplateLiteral | TypeFlags.StringMapping) && !isPatternLiteralType(type) ? ObjectFlags.IsGenericIndexType : 0);
1809918105
}
1810018106

1810118107
function getSimplifiedType(type: Type, writing: boolean): Type {
@@ -24767,7 +24773,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
2476724773
objectFlags & ObjectFlags.Anonymous && type.symbol && type.symbol.flags & (SymbolFlags.Function | SymbolFlags.Method | SymbolFlags.Class | SymbolFlags.TypeLiteral | SymbolFlags.ObjectLiteral) && type.symbol.declarations ||
2476824774
objectFlags & (ObjectFlags.Mapped | ObjectFlags.ReverseMapped | ObjectFlags.ObjectRestType | ObjectFlags.InstantiationExpressionType)
2476924775
) ||
24770-
type.flags & (TypeFlags.UnionOrIntersection | TypeFlags.TemplateLiteral) && !(type.flags & TypeFlags.EnumLiteral) && !isNonGenericTopLevelType(type) && some((type as UnionOrIntersectionType | TemplateLiteralType).types, couldContainTypeVariables));
24776+
type.flags & TypeFlags.UnionOrIntersection && !(type.flags & TypeFlags.EnumLiteral) && !isNonGenericTopLevelType(type) && some((type as UnionOrIntersectionType).types, couldContainTypeVariables));
2477124777
if (type.flags & TypeFlags.ObjectFlagsType) {
2477224778
(type as ObjectFlagsType).objectFlags |= ObjectFlags.CouldContainTypeVariablesComputed | (result ? ObjectFlags.CouldContainTypeVariables : 0);
2477324779
}

src/compiler/types.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6130,7 +6130,7 @@ export const enum TypeFlags {
61306130
Instantiable = InstantiableNonPrimitive | InstantiablePrimitive,
61316131
StructuredOrInstantiable = StructuredType | Instantiable,
61326132
/** @internal */
6133-
ObjectFlagsType = Any | Nullable | Never | Object | Union | Intersection | TemplateLiteral,
6133+
ObjectFlagsType = Any | Nullable | Never | Object | Union | Intersection,
61346134
/** @internal */
61356135
Simplifiable = IndexedAccess | Conditional,
61366136
/** @internal */
@@ -6289,7 +6289,7 @@ export const enum ObjectFlags {
62896289
/** @internal */
62906290
IdenticalBaseTypeExists = 1 << 26, // has a defined cachedEquivalentBaseType member
62916291

6292-
// Flags that require TypeFlags.UnionOrIntersection, TypeFlags.Substitution, or TypeFlags.TemplateLiteral
6292+
// Flags that require TypeFlags.UnionOrIntersection or TypeFlags.Substitution
62936293
/** @internal */
62946294
IsGenericTypeComputed = 1 << 21, // IsGenericObjectType flag has been computed
62956295
/** @internal */
@@ -6316,7 +6316,7 @@ export const enum ObjectFlags {
63166316
}
63176317

63186318
/** @internal */
6319-
export type ObjectFlagsType = NullableType | ObjectType | UnionType | IntersectionType | TemplateLiteralType;
6319+
export type ObjectFlagsType = NullableType | ObjectType | UnionType | IntersectionType;
63206320

63216321
// Object types (TypeFlags.ObjectType)
63226322
// dprint-ignore
@@ -6675,8 +6675,6 @@ export interface ConditionalType extends InstantiableType {
66756675
}
66766676

66776677
export interface TemplateLiteralType extends InstantiableType {
6678-
/** @internal */
6679-
objectFlags: ObjectFlags;
66806678
texts: readonly string[]; // Always one element longer than types
66816679
types: readonly Type[]; // Always at least one element
66826680
}

tests/baselines/reference/templateLiteralTypesPatterns.errors.txt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ templateLiteralTypesPatterns.ts(129,9): error TS2345: Argument of type '"1.1e-10
5555
templateLiteralTypesPatterns.ts(140,1): error TS2322: Type '`a${string}`' is not assignable to type '`a${number}`'.
5656
templateLiteralTypesPatterns.ts(141,1): error TS2322: Type '"bno"' is not assignable to type '`a${any}`'.
5757
templateLiteralTypesPatterns.ts(160,7): error TS2322: Type '"anything"' is not assignable to type '`${number} ${number}`'.
58-
templateLiteralTypesPatterns.ts(211,5): error TS2345: Argument of type '"abcTest"' is not assignable to parameter of type '`${`a${string}` & `${string}a`}Test`'.
58+
templateLiteralTypesPatterns.ts(215,5): error TS2345: Argument of type '"abcTest"' is not assignable to parameter of type '`${`a${string}` & `${string}a`}Test`'.
5959

6060

6161
==== templateLiteralTypesPatterns.ts (58 errors) ====
@@ -376,10 +376,14 @@ templateLiteralTypesPatterns.ts(211,5): error TS2345: Argument of type '"abcTest
376376
}
377377

378378
// repro from https://github.com/microsoft/TypeScript/issues/54177#issuecomment-1538436654
379-
function conversionTest(groupName: | "downcast" | "dataDowncast" | "editingDowncast" | `${string & {}}Downcast`) {}
379+
function conversionTest(groupName: | "downcast" | "dataDowncast" | "editingDowncast" | `${string}Downcast` & {}) {}
380380
conversionTest("testDowncast");
381-
function conversionTest2(groupName: | "downcast" | "dataDowncast" | "editingDowncast" | `${{} & string}Downcast`) {}
381+
function conversionTest2(groupName: | "downcast" | "dataDowncast" | "editingDowncast" | {} & `${string}Downcast`) {}
382382
conversionTest2("testDowncast");
383+
function conversionTest3(groupName: | "downcast" | "dataDowncast" | "editingDowncast" | `${string & {}}Downcast`) {}
384+
conversionTest3("testDowncast");
385+
function conversionTest4(groupName: | "downcast" | "dataDowncast" | "editingDowncast" | `${{} & string}Downcast`) {}
386+
conversionTest4("testDowncast");
383387

384388
function foo(str: `${`a${string}` & `${string}a`}Test`) {}
385389
foo("abaTest"); // ok

tests/baselines/reference/templateLiteralTypesPatterns.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,14 @@ export abstract class BB {
204204
}
205205

206206
// repro from https://github.com/microsoft/TypeScript/issues/54177#issuecomment-1538436654
207-
function conversionTest(groupName: | "downcast" | "dataDowncast" | "editingDowncast" | `${string & {}}Downcast`) {}
207+
function conversionTest(groupName: | "downcast" | "dataDowncast" | "editingDowncast" | `${string}Downcast` & {}) {}
208208
conversionTest("testDowncast");
209-
function conversionTest2(groupName: | "downcast" | "dataDowncast" | "editingDowncast" | `${{} & string}Downcast`) {}
209+
function conversionTest2(groupName: | "downcast" | "dataDowncast" | "editingDowncast" | {} & `${string}Downcast`) {}
210210
conversionTest2("testDowncast");
211+
function conversionTest3(groupName: | "downcast" | "dataDowncast" | "editingDowncast" | `${string & {}}Downcast`) {}
212+
conversionTest3("testDowncast");
213+
function conversionTest4(groupName: | "downcast" | "dataDowncast" | "editingDowncast" | `${{} & string}Downcast`) {}
214+
conversionTest4("testDowncast");
211215

212216
function foo(str: `${`a${string}` & `${string}a`}Test`) {}
213217
foo("abaTest"); // ok
@@ -367,6 +371,10 @@ function conversionTest(groupName) { }
367371
conversionTest("testDowncast");
368372
function conversionTest2(groupName) { }
369373
conversionTest2("testDowncast");
374+
function conversionTest3(groupName) { }
375+
conversionTest3("testDowncast");
376+
function conversionTest4(groupName) { }
377+
conversionTest4("testDowncast");
370378
function foo(str) { }
371379
foo("abaTest"); // ok
372380
foo("abcTest"); // error

tests/baselines/reference/templateLiteralTypesPatterns.symbols

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -488,27 +488,41 @@ export abstract class BB {
488488
}
489489

490490
// repro from https://github.com/microsoft/TypeScript/issues/54177#issuecomment-1538436654
491-
function conversionTest(groupName: | "downcast" | "dataDowncast" | "editingDowncast" | `${string & {}}Downcast`) {}
491+
function conversionTest(groupName: | "downcast" | "dataDowncast" | "editingDowncast" | `${string}Downcast` & {}) {}
492492
>conversionTest : Symbol(conversionTest, Decl(templateLiteralTypesPatterns.ts, 200, 1))
493493
>groupName : Symbol(groupName, Decl(templateLiteralTypesPatterns.ts, 203, 24))
494494

495495
conversionTest("testDowncast");
496496
>conversionTest : Symbol(conversionTest, Decl(templateLiteralTypesPatterns.ts, 200, 1))
497497

498-
function conversionTest2(groupName: | "downcast" | "dataDowncast" | "editingDowncast" | `${{} & string}Downcast`) {}
498+
function conversionTest2(groupName: | "downcast" | "dataDowncast" | "editingDowncast" | {} & `${string}Downcast`) {}
499499
>conversionTest2 : Symbol(conversionTest2, Decl(templateLiteralTypesPatterns.ts, 204, 31))
500500
>groupName : Symbol(groupName, Decl(templateLiteralTypesPatterns.ts, 205, 25))
501501

502502
conversionTest2("testDowncast");
503503
>conversionTest2 : Symbol(conversionTest2, Decl(templateLiteralTypesPatterns.ts, 204, 31))
504504

505+
function conversionTest3(groupName: | "downcast" | "dataDowncast" | "editingDowncast" | `${string & {}}Downcast`) {}
506+
>conversionTest3 : Symbol(conversionTest3, Decl(templateLiteralTypesPatterns.ts, 206, 32))
507+
>groupName : Symbol(groupName, Decl(templateLiteralTypesPatterns.ts, 207, 25))
508+
509+
conversionTest3("testDowncast");
510+
>conversionTest3 : Symbol(conversionTest3, Decl(templateLiteralTypesPatterns.ts, 206, 32))
511+
512+
function conversionTest4(groupName: | "downcast" | "dataDowncast" | "editingDowncast" | `${{} & string}Downcast`) {}
513+
>conversionTest4 : Symbol(conversionTest4, Decl(templateLiteralTypesPatterns.ts, 208, 32))
514+
>groupName : Symbol(groupName, Decl(templateLiteralTypesPatterns.ts, 209, 25))
515+
516+
conversionTest4("testDowncast");
517+
>conversionTest4 : Symbol(conversionTest4, Decl(templateLiteralTypesPatterns.ts, 208, 32))
518+
505519
function foo(str: `${`a${string}` & `${string}a`}Test`) {}
506-
>foo : Symbol(foo, Decl(templateLiteralTypesPatterns.ts, 206, 32))
507-
>str : Symbol(str, Decl(templateLiteralTypesPatterns.ts, 208, 13))
520+
>foo : Symbol(foo, Decl(templateLiteralTypesPatterns.ts, 210, 32))
521+
>str : Symbol(str, Decl(templateLiteralTypesPatterns.ts, 212, 13))
508522

509523
foo("abaTest"); // ok
510-
>foo : Symbol(foo, Decl(templateLiteralTypesPatterns.ts, 206, 32))
524+
>foo : Symbol(foo, Decl(templateLiteralTypesPatterns.ts, 210, 32))
511525

512526
foo("abcTest"); // error
513-
>foo : Symbol(foo, Decl(templateLiteralTypesPatterns.ts, 206, 32))
527+
>foo : Symbol(foo, Decl(templateLiteralTypesPatterns.ts, 210, 32))
514528

tests/baselines/reference/templateLiteralTypesPatterns.types

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -636,22 +636,40 @@ export abstract class BB {
636636
}
637637

638638
// repro from https://github.com/microsoft/TypeScript/issues/54177#issuecomment-1538436654
639-
function conversionTest(groupName: | "downcast" | "dataDowncast" | "editingDowncast" | `${string & {}}Downcast`) {}
640-
>conversionTest : (groupName: "downcast" | "dataDowncast" | "editingDowncast" | `${string & {}}Downcast`) => void
641-
>groupName : `${string & {}}Downcast` | "downcast" | "dataDowncast" | "editingDowncast"
639+
function conversionTest(groupName: | "downcast" | "dataDowncast" | "editingDowncast" | `${string}Downcast` & {}) {}
640+
>conversionTest : (groupName: "downcast" | "dataDowncast" | "editingDowncast" | `${string}Downcast` & {}) => void
641+
>groupName : (`${string}Downcast` & {}) | "downcast" | "dataDowncast" | "editingDowncast"
642642

643643
conversionTest("testDowncast");
644644
>conversionTest("testDowncast") : void
645-
>conversionTest : (groupName: `${string & {}}Downcast` | "downcast" | "dataDowncast" | "editingDowncast") => void
645+
>conversionTest : (groupName: (`${string}Downcast` & {}) | "downcast" | "dataDowncast" | "editingDowncast") => void
646646
>"testDowncast" : "testDowncast"
647647

648-
function conversionTest2(groupName: | "downcast" | "dataDowncast" | "editingDowncast" | `${{} & string}Downcast`) {}
649-
>conversionTest2 : (groupName: "downcast" | "dataDowncast" | "editingDowncast" | `${{} & string}Downcast`) => void
650-
>groupName : "downcast" | "dataDowncast" | "editingDowncast" | `${{} & string}Downcast`
648+
function conversionTest2(groupName: | "downcast" | "dataDowncast" | "editingDowncast" | {} & `${string}Downcast`) {}
649+
>conversionTest2 : (groupName: "downcast" | "dataDowncast" | "editingDowncast" | {} & `${string}Downcast`) => void
650+
>groupName : "downcast" | "dataDowncast" | "editingDowncast" | ({} & `${string}Downcast`)
651651

652652
conversionTest2("testDowncast");
653653
>conversionTest2("testDowncast") : void
654-
>conversionTest2 : (groupName: "downcast" | "dataDowncast" | "editingDowncast" | `${{} & string}Downcast`) => void
654+
>conversionTest2 : (groupName: "downcast" | "dataDowncast" | "editingDowncast" | ({} & `${string}Downcast`)) => void
655+
>"testDowncast" : "testDowncast"
656+
657+
function conversionTest3(groupName: | "downcast" | "dataDowncast" | "editingDowncast" | `${string & {}}Downcast`) {}
658+
>conversionTest3 : (groupName: "downcast" | "dataDowncast" | "editingDowncast" | `${string & {}}Downcast`) => void
659+
>groupName : "downcast" | `${string & {}}Downcast`
660+
661+
conversionTest3("testDowncast");
662+
>conversionTest3("testDowncast") : void
663+
>conversionTest3 : (groupName: "downcast" | `${string & {}}Downcast`) => void
664+
>"testDowncast" : "testDowncast"
665+
666+
function conversionTest4(groupName: | "downcast" | "dataDowncast" | "editingDowncast" | `${{} & string}Downcast`) {}
667+
>conversionTest4 : (groupName: "downcast" | "dataDowncast" | "editingDowncast" | `${{} & string}Downcast`) => void
668+
>groupName : "downcast" | `${{} & string}Downcast`
669+
670+
conversionTest4("testDowncast");
671+
>conversionTest4("testDowncast") : void
672+
>conversionTest4 : (groupName: "downcast" | `${{} & string}Downcast`) => void
655673
>"testDowncast" : "testDowncast"
656674

657675
function foo(str: `${`a${string}` & `${string}a`}Test`) {}

0 commit comments

Comments
 (0)