From 7fa9179724edccaa10cecfb358fbfcfa8a43e043 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Thu, 16 Nov 2023 09:27:38 -0800 Subject: [PATCH 1/6] Fix support for intersections in template literal placeholder types --- src/compiler/checker.ts | 50 +++++++++++++++++++++++------------------ src/compiler/types.ts | 8 +++---- 2 files changed, 31 insertions(+), 27 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index f27a6058006e5..ff98abfce7bbf 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -16925,10 +16925,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { } function removeStringLiteralsMatchedByTemplateLiterals(types: Type[]) { - const templates = filter(types, t => - !!(t.flags & TypeFlags.TemplateLiteral) && - isPatternLiteralType(t) && - (t as TemplateLiteralType).types.every(t => !(t.flags & TypeFlags.Intersection) || !areIntersectedTypesAvoidingPrimitiveReduction((t as IntersectionType).types))) as TemplateLiteralType[]; + const templates = filter(types, t => !!(t.flags & TypeFlags.TemplateLiteral) && isPatternLiteralType(t)) as TemplateLiteralType[]; if (templates.length) { let i = types.length; while (i > 0) { @@ -17439,20 +17436,17 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { return reduceLeft(types, (n, t) => n + getConstituentCount(t), 0); } - function areIntersectedTypesAvoidingPrimitiveReduction(types: Type[], primitiveFlags = TypeFlags.String | TypeFlags.Number | TypeFlags.BigInt): boolean { - if (types.length !== 2) { - return false; - } - const [t1, t2] = types; - return !!(t1.flags & primitiveFlags) && t2 === emptyTypeLiteralType || !!(t2.flags & primitiveFlags) && t1 === emptyTypeLiteralType; - } - function getTypeFromIntersectionTypeNode(node: IntersectionTypeNode): Type { const links = getNodeLinks(node); if (!links.resolvedType) { const aliasSymbol = getAliasSymbolForTypeNode(node); const types = map(node.types, getTypeFromTypeNode); - const noSupertypeReduction = areIntersectedTypesAvoidingPrimitiveReduction(types); + // We perform no supertype reduction for X & {} or {} & X, where X is one of string, number, bigint, + // or a pattern literal template type. This enables union types like "a" | "b" | string & {} or + // "aa" | "ab" | `a${string}` which preserve the literal types for purposes of statement completion. + const emptyIndex = types.length === 2 ? types.indexOf(emptyTypeLiteralType) : -1; + const t = emptyIndex >= 0 ? types[1 - emptyIndex] : unknownType; + const noSupertypeReduction = !!(t.flags & (TypeFlags.String | TypeFlags.Number | TypeFlags.BigInt) || t.flags & TypeFlags.TemplateLiteral && isPatternLiteralType(t)); links.resolvedType = getIntersectionType(types, aliasSymbol, getTypeArgumentsForAliasSymbol(aliasSymbol), noSupertypeReduction); } return links.resolvedType; @@ -17735,7 +17729,6 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { function createTemplateLiteralType(texts: readonly string[], types: readonly Type[]) { const type = createType(TypeFlags.TemplateLiteral) as TemplateLiteralType; - type.objectFlags = getPropagatingFlagsOfTypes(types, /*excludeKinds*/ TypeFlags.Nullable); type.texts = texts; type.types = types; return type; @@ -18060,12 +18053,25 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { function isPatternLiteralPlaceholderType(type: Type): boolean { if (type.flags & TypeFlags.Intersection) { - return !isGenericType(type) && some((type as IntersectionType).types, t => !!(t.flags & (TypeFlags.Literal | TypeFlags.Nullable)) || isPatternLiteralPlaceholderType(t)); + // Return true if the intersection consists of one or more placeholders and zero or + // more object type tags. + let seenPlaceholder = false; + for (const t of (type as IntersectionType).types) { + if (t.flags & (TypeFlags.Literal | TypeFlags.Nullable) || isPatternLiteralPlaceholderType(t)) { + seenPlaceholder = true; + } + else if (!(t.flags & TypeFlags.Object)) { + return false; + } + } + return seenPlaceholder; } return !!(type.flags & (TypeFlags.Any | TypeFlags.String | TypeFlags.Number | TypeFlags.BigInt)) || isPatternLiteralType(type); } function isPatternLiteralType(type: Type) { + // A pattern literal type is a template literal or a string mapping type that contains only + // non-generic pattern literal placeholders. return !!(type.flags & TypeFlags.TemplateLiteral) && every((type as TemplateLiteralType).types, isPatternLiteralPlaceholderType) || !!(type.flags & TypeFlags.StringMapping) && isPatternLiteralPlaceholderType((type as StringMappingType).type); } @@ -18083,12 +18089,12 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { } function getGenericObjectFlags(type: Type): ObjectFlags { - if (type.flags & (TypeFlags.UnionOrIntersection | TypeFlags.TemplateLiteral)) { - if (!((type as UnionOrIntersectionType | TemplateLiteralType).objectFlags & ObjectFlags.IsGenericTypeComputed)) { - (type as UnionOrIntersectionType | TemplateLiteralType).objectFlags |= ObjectFlags.IsGenericTypeComputed | - reduceLeft((type as UnionOrIntersectionType | TemplateLiteralType).types, (flags, t) => flags | getGenericObjectFlags(t), 0); + if (type.flags & (TypeFlags.UnionOrIntersection)) { + if (!((type as UnionOrIntersectionType).objectFlags & ObjectFlags.IsGenericTypeComputed)) { + (type as UnionOrIntersectionType).objectFlags |= ObjectFlags.IsGenericTypeComputed | + reduceLeft((type as UnionOrIntersectionType).types, (flags, t) => flags | getGenericObjectFlags(t), 0); } - return (type as UnionOrIntersectionType | TemplateLiteralType).objectFlags & ObjectFlags.IsGenericType; + return (type as UnionOrIntersectionType).objectFlags & ObjectFlags.IsGenericType; } if (type.flags & TypeFlags.Substitution) { if (!((type as SubstitutionType).objectFlags & ObjectFlags.IsGenericTypeComputed)) { @@ -18098,7 +18104,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { return (type as SubstitutionType).objectFlags & ObjectFlags.IsGenericType; } return (type.flags & TypeFlags.InstantiableNonPrimitive || isGenericMappedType(type) || isGenericTupleType(type) ? ObjectFlags.IsGenericObjectType : 0) | - (type.flags & (TypeFlags.InstantiableNonPrimitive | TypeFlags.Index | TypeFlags.StringMapping) && !isPatternLiteralType(type) ? ObjectFlags.IsGenericIndexType : 0); + (type.flags & (TypeFlags.InstantiableNonPrimitive | TypeFlags.Index | TypeFlags.TemplateLiteral | TypeFlags.StringMapping) && !isPatternLiteralType(type) ? ObjectFlags.IsGenericIndexType : 0); } function getSimplifiedType(type: Type, writing: boolean): Type { @@ -24768,7 +24774,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { objectFlags & ObjectFlags.Anonymous && type.symbol && type.symbol.flags & (SymbolFlags.Function | SymbolFlags.Method | SymbolFlags.Class | SymbolFlags.TypeLiteral | SymbolFlags.ObjectLiteral) && type.symbol.declarations || objectFlags & (ObjectFlags.Mapped | ObjectFlags.ReverseMapped | ObjectFlags.ObjectRestType | ObjectFlags.InstantiationExpressionType) ) || - type.flags & (TypeFlags.UnionOrIntersection | TypeFlags.TemplateLiteral) && !(type.flags & TypeFlags.EnumLiteral) && !isNonGenericTopLevelType(type) && some((type as UnionOrIntersectionType | TemplateLiteralType).types, couldContainTypeVariables)); + type.flags & TypeFlags.UnionOrIntersection && !(type.flags & TypeFlags.EnumLiteral) && !isNonGenericTopLevelType(type) && some((type as UnionOrIntersectionType).types, couldContainTypeVariables)); if (type.flags & TypeFlags.ObjectFlagsType) { (type as ObjectFlagsType).objectFlags |= ObjectFlags.CouldContainTypeVariablesComputed | (result ? ObjectFlags.CouldContainTypeVariables : 0); } diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 34a2835822302..d96277b936052 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -6130,7 +6130,7 @@ export const enum TypeFlags { Instantiable = InstantiableNonPrimitive | InstantiablePrimitive, StructuredOrInstantiable = StructuredType | Instantiable, /** @internal */ - ObjectFlagsType = Any | Nullable | Never | Object | Union | Intersection | TemplateLiteral, + ObjectFlagsType = Any | Nullable | Never | Object | Union | Intersection, /** @internal */ Simplifiable = IndexedAccess | Conditional, /** @internal */ @@ -6289,7 +6289,7 @@ export const enum ObjectFlags { /** @internal */ IdenticalBaseTypeExists = 1 << 26, // has a defined cachedEquivalentBaseType member - // Flags that require TypeFlags.UnionOrIntersection, TypeFlags.Substitution, or TypeFlags.TemplateLiteral + // Flags that require TypeFlags.UnionOrIntersection or TypeFlags.Substitution /** @internal */ IsGenericTypeComputed = 1 << 21, // IsGenericObjectType flag has been computed /** @internal */ @@ -6316,7 +6316,7 @@ export const enum ObjectFlags { } /** @internal */ -export type ObjectFlagsType = NullableType | ObjectType | UnionType | IntersectionType | TemplateLiteralType; +export type ObjectFlagsType = NullableType | ObjectType | UnionType | IntersectionType; // Object types (TypeFlags.ObjectType) // dprint-ignore @@ -6675,8 +6675,6 @@ export interface ConditionalType extends InstantiableType { } export interface TemplateLiteralType extends InstantiableType { - /** @internal */ - objectFlags: ObjectFlags; texts: readonly string[]; // Always one element longer than types types: readonly Type[]; // Always at least one element } From f45b6dd7125ac9b2313739302dc43080b981cdcf Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Thu, 16 Nov 2023 09:28:13 -0800 Subject: [PATCH 2/6] Accept new baselines --- .../reference/templateLiteralTypesPatterns.types | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/baselines/reference/templateLiteralTypesPatterns.types b/tests/baselines/reference/templateLiteralTypesPatterns.types index d9fbfeec9d8b2..259d1e591bc79 100644 --- a/tests/baselines/reference/templateLiteralTypesPatterns.types +++ b/tests/baselines/reference/templateLiteralTypesPatterns.types @@ -638,20 +638,20 @@ export abstract class BB { // repro from https://github.com/microsoft/TypeScript/issues/54177#issuecomment-1538436654 function conversionTest(groupName: | "downcast" | "dataDowncast" | "editingDowncast" | `${string & {}}Downcast`) {} >conversionTest : (groupName: "downcast" | "dataDowncast" | "editingDowncast" | `${string & {}}Downcast`) => void ->groupName : `${string & {}}Downcast` | "downcast" | "dataDowncast" | "editingDowncast" +>groupName : `${string & {}}Downcast` | "downcast" conversionTest("testDowncast"); >conversionTest("testDowncast") : void ->conversionTest : (groupName: `${string & {}}Downcast` | "downcast" | "dataDowncast" | "editingDowncast") => void +>conversionTest : (groupName: `${string & {}}Downcast` | "downcast") => void >"testDowncast" : "testDowncast" function conversionTest2(groupName: | "downcast" | "dataDowncast" | "editingDowncast" | `${{} & string}Downcast`) {} >conversionTest2 : (groupName: "downcast" | "dataDowncast" | "editingDowncast" | `${{} & string}Downcast`) => void ->groupName : "downcast" | "dataDowncast" | "editingDowncast" | `${{} & string}Downcast` +>groupName : "downcast" | `${{} & string}Downcast` conversionTest2("testDowncast"); >conversionTest2("testDowncast") : void ->conversionTest2 : (groupName: "downcast" | "dataDowncast" | "editingDowncast" | `${{} & string}Downcast`) => void +>conversionTest2 : (groupName: "downcast" | `${{} & string}Downcast`) => void >"testDowncast" : "testDowncast" function foo(str: `${`a${string}` & `${string}a`}Test`) {} From 2cf837931040ab047368136e13e252ea5db71b32 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Thu, 16 Nov 2023 09:32:58 -0800 Subject: [PATCH 3/6] Update test --- .../conformance/types/literal/templateLiteralTypesPatterns.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/cases/conformance/types/literal/templateLiteralTypesPatterns.ts b/tests/cases/conformance/types/literal/templateLiteralTypesPatterns.ts index aeb05ead7ccca..ada8e7f9f17ca 100644 --- a/tests/cases/conformance/types/literal/templateLiteralTypesPatterns.ts +++ b/tests/cases/conformance/types/literal/templateLiteralTypesPatterns.ts @@ -202,9 +202,9 @@ export abstract class BB { } // repro from https://github.com/microsoft/TypeScript/issues/54177#issuecomment-1538436654 -function conversionTest(groupName: | "downcast" | "dataDowncast" | "editingDowncast" | `${string & {}}Downcast`) {} +function conversionTest(groupName: | "downcast" | "dataDowncast" | "editingDowncast" | `${string}Downcast` & {}) {} conversionTest("testDowncast"); -function conversionTest2(groupName: | "downcast" | "dataDowncast" | "editingDowncast" | `${{} & string}Downcast`) {} +function conversionTest2(groupName: | "downcast" | "dataDowncast" | "editingDowncast" | {} & `${string}Downcast`) {} conversionTest2("testDowncast"); function foo(str: `${`a${string}` & `${string}a`}Test`) {} From ca7e9c4d1d07145d61400504763dfe49278615a6 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Thu, 16 Nov 2023 09:33:06 -0800 Subject: [PATCH 4/6] Accept new baselines --- .../templateLiteralTypesPatterns.errors.txt | 4 ++-- .../reference/templateLiteralTypesPatterns.js | 4 ++-- .../templateLiteralTypesPatterns.symbols | 4 ++-- .../reference/templateLiteralTypesPatterns.types | 16 ++++++++-------- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/tests/baselines/reference/templateLiteralTypesPatterns.errors.txt b/tests/baselines/reference/templateLiteralTypesPatterns.errors.txt index cd82afc83981b..7bc21757b5f6e 100644 --- a/tests/baselines/reference/templateLiteralTypesPatterns.errors.txt +++ b/tests/baselines/reference/templateLiteralTypesPatterns.errors.txt @@ -376,9 +376,9 @@ templateLiteralTypesPatterns.ts(211,5): error TS2345: Argument of type '"abcTest } // repro from https://github.com/microsoft/TypeScript/issues/54177#issuecomment-1538436654 - function conversionTest(groupName: | "downcast" | "dataDowncast" | "editingDowncast" | `${string & {}}Downcast`) {} + function conversionTest(groupName: | "downcast" | "dataDowncast" | "editingDowncast" | `${string}Downcast` & {}) {} conversionTest("testDowncast"); - function conversionTest2(groupName: | "downcast" | "dataDowncast" | "editingDowncast" | `${{} & string}Downcast`) {} + function conversionTest2(groupName: | "downcast" | "dataDowncast" | "editingDowncast" | {} & `${string}Downcast`) {} conversionTest2("testDowncast"); function foo(str: `${`a${string}` & `${string}a`}Test`) {} diff --git a/tests/baselines/reference/templateLiteralTypesPatterns.js b/tests/baselines/reference/templateLiteralTypesPatterns.js index d66c794e287a8..846a265e8e8b6 100644 --- a/tests/baselines/reference/templateLiteralTypesPatterns.js +++ b/tests/baselines/reference/templateLiteralTypesPatterns.js @@ -204,9 +204,9 @@ export abstract class BB { } // repro from https://github.com/microsoft/TypeScript/issues/54177#issuecomment-1538436654 -function conversionTest(groupName: | "downcast" | "dataDowncast" | "editingDowncast" | `${string & {}}Downcast`) {} +function conversionTest(groupName: | "downcast" | "dataDowncast" | "editingDowncast" | `${string}Downcast` & {}) {} conversionTest("testDowncast"); -function conversionTest2(groupName: | "downcast" | "dataDowncast" | "editingDowncast" | `${{} & string}Downcast`) {} +function conversionTest2(groupName: | "downcast" | "dataDowncast" | "editingDowncast" | {} & `${string}Downcast`) {} conversionTest2("testDowncast"); function foo(str: `${`a${string}` & `${string}a`}Test`) {} diff --git a/tests/baselines/reference/templateLiteralTypesPatterns.symbols b/tests/baselines/reference/templateLiteralTypesPatterns.symbols index cc508d2680959..fe3b75119c772 100644 --- a/tests/baselines/reference/templateLiteralTypesPatterns.symbols +++ b/tests/baselines/reference/templateLiteralTypesPatterns.symbols @@ -488,14 +488,14 @@ export abstract class BB { } // repro from https://github.com/microsoft/TypeScript/issues/54177#issuecomment-1538436654 -function conversionTest(groupName: | "downcast" | "dataDowncast" | "editingDowncast" | `${string & {}}Downcast`) {} +function conversionTest(groupName: | "downcast" | "dataDowncast" | "editingDowncast" | `${string}Downcast` & {}) {} >conversionTest : Symbol(conversionTest, Decl(templateLiteralTypesPatterns.ts, 200, 1)) >groupName : Symbol(groupName, Decl(templateLiteralTypesPatterns.ts, 203, 24)) conversionTest("testDowncast"); >conversionTest : Symbol(conversionTest, Decl(templateLiteralTypesPatterns.ts, 200, 1)) -function conversionTest2(groupName: | "downcast" | "dataDowncast" | "editingDowncast" | `${{} & string}Downcast`) {} +function conversionTest2(groupName: | "downcast" | "dataDowncast" | "editingDowncast" | {} & `${string}Downcast`) {} >conversionTest2 : Symbol(conversionTest2, Decl(templateLiteralTypesPatterns.ts, 204, 31)) >groupName : Symbol(groupName, Decl(templateLiteralTypesPatterns.ts, 205, 25)) diff --git a/tests/baselines/reference/templateLiteralTypesPatterns.types b/tests/baselines/reference/templateLiteralTypesPatterns.types index 259d1e591bc79..88864ad046ba2 100644 --- a/tests/baselines/reference/templateLiteralTypesPatterns.types +++ b/tests/baselines/reference/templateLiteralTypesPatterns.types @@ -636,22 +636,22 @@ export abstract class BB { } // repro from https://github.com/microsoft/TypeScript/issues/54177#issuecomment-1538436654 -function conversionTest(groupName: | "downcast" | "dataDowncast" | "editingDowncast" | `${string & {}}Downcast`) {} ->conversionTest : (groupName: "downcast" | "dataDowncast" | "editingDowncast" | `${string & {}}Downcast`) => void ->groupName : `${string & {}}Downcast` | "downcast" +function conversionTest(groupName: | "downcast" | "dataDowncast" | "editingDowncast" | `${string}Downcast` & {}) {} +>conversionTest : (groupName: "downcast" | "dataDowncast" | "editingDowncast" | `${string}Downcast` & {}) => void +>groupName : (`${string}Downcast` & {}) | "downcast" | "dataDowncast" | "editingDowncast" conversionTest("testDowncast"); >conversionTest("testDowncast") : void ->conversionTest : (groupName: `${string & {}}Downcast` | "downcast") => void +>conversionTest : (groupName: (`${string}Downcast` & {}) | "downcast" | "dataDowncast" | "editingDowncast") => void >"testDowncast" : "testDowncast" -function conversionTest2(groupName: | "downcast" | "dataDowncast" | "editingDowncast" | `${{} & string}Downcast`) {} ->conversionTest2 : (groupName: "downcast" | "dataDowncast" | "editingDowncast" | `${{} & string}Downcast`) => void ->groupName : "downcast" | `${{} & string}Downcast` +function conversionTest2(groupName: | "downcast" | "dataDowncast" | "editingDowncast" | {} & `${string}Downcast`) {} +>conversionTest2 : (groupName: "downcast" | "dataDowncast" | "editingDowncast" | {} & `${string}Downcast`) => void +>groupName : "downcast" | "dataDowncast" | "editingDowncast" | ({} & `${string}Downcast`) conversionTest2("testDowncast"); >conversionTest2("testDowncast") : void ->conversionTest2 : (groupName: "downcast" | `${{} & string}Downcast`) => void +>conversionTest2 : (groupName: "downcast" | "dataDowncast" | "editingDowncast" | ({} & `${string}Downcast`)) => void >"testDowncast" : "testDowncast" function foo(str: `${`a${string}` & `${string}a`}Test`) {} From 8ab01ebf6d544219119e9d848461bf9d6f79012f Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Thu, 16 Nov 2023 09:53:09 -0800 Subject: [PATCH 5/6] Update fourslash test --- .../stringLiteralCompletionsForOpenEndedTemplateLiteralType.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/cases/fourslash/stringLiteralCompletionsForOpenEndedTemplateLiteralType.ts b/tests/cases/fourslash/stringLiteralCompletionsForOpenEndedTemplateLiteralType.ts index 92c02a23a2854..c5472fab365ad 100644 --- a/tests/cases/fourslash/stringLiteralCompletionsForOpenEndedTemplateLiteralType.ts +++ b/tests/cases/fourslash/stringLiteralCompletionsForOpenEndedTemplateLiteralType.ts @@ -1,6 +1,6 @@ /// -//// function conversionTest(groupName: | "downcast" | "dataDowncast" | "editingDowncast" | `${string & {}}Downcast`) {} +//// function conversionTest(groupName: | "downcast" | "dataDowncast" | "editingDowncast" | `${string}Downcast` & {}) {} //// conversionTest("/**/"); verify.completions({ marker: "", exact: ["downcast", "dataDowncast", "editingDowncast"] }); From b464619504ef607b06a4725258f68506449c76a8 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Thu, 16 Nov 2023 13:43:29 -0800 Subject: [PATCH 6/6] Add more tests per code review --- .../templateLiteralTypesPatterns.errors.txt | 6 ++++- .../reference/templateLiteralTypesPatterns.js | 8 +++++++ .../templateLiteralTypesPatterns.symbols | 22 +++++++++++++++---- .../templateLiteralTypesPatterns.types | 18 +++++++++++++++ .../literal/templateLiteralTypesPatterns.ts | 4 ++++ 5 files changed, 53 insertions(+), 5 deletions(-) diff --git a/tests/baselines/reference/templateLiteralTypesPatterns.errors.txt b/tests/baselines/reference/templateLiteralTypesPatterns.errors.txt index 7bc21757b5f6e..3ead220eb04cd 100644 --- a/tests/baselines/reference/templateLiteralTypesPatterns.errors.txt +++ b/tests/baselines/reference/templateLiteralTypesPatterns.errors.txt @@ -55,7 +55,7 @@ templateLiteralTypesPatterns.ts(129,9): error TS2345: Argument of type '"1.1e-10 templateLiteralTypesPatterns.ts(140,1): error TS2322: Type '`a${string}`' is not assignable to type '`a${number}`'. templateLiteralTypesPatterns.ts(141,1): error TS2322: Type '"bno"' is not assignable to type '`a${any}`'. templateLiteralTypesPatterns.ts(160,7): error TS2322: Type '"anything"' is not assignable to type '`${number} ${number}`'. -templateLiteralTypesPatterns.ts(211,5): error TS2345: Argument of type '"abcTest"' is not assignable to parameter of type '`${`a${string}` & `${string}a`}Test`'. +templateLiteralTypesPatterns.ts(215,5): error TS2345: Argument of type '"abcTest"' is not assignable to parameter of type '`${`a${string}` & `${string}a`}Test`'. ==== templateLiteralTypesPatterns.ts (58 errors) ==== @@ -380,6 +380,10 @@ templateLiteralTypesPatterns.ts(211,5): error TS2345: Argument of type '"abcTest conversionTest("testDowncast"); function conversionTest2(groupName: | "downcast" | "dataDowncast" | "editingDowncast" | {} & `${string}Downcast`) {} conversionTest2("testDowncast"); + function conversionTest3(groupName: | "downcast" | "dataDowncast" | "editingDowncast" | `${string & {}}Downcast`) {} + conversionTest3("testDowncast"); + function conversionTest4(groupName: | "downcast" | "dataDowncast" | "editingDowncast" | `${{} & string}Downcast`) {} + conversionTest4("testDowncast"); function foo(str: `${`a${string}` & `${string}a`}Test`) {} foo("abaTest"); // ok diff --git a/tests/baselines/reference/templateLiteralTypesPatterns.js b/tests/baselines/reference/templateLiteralTypesPatterns.js index 846a265e8e8b6..8741dda63c2cc 100644 --- a/tests/baselines/reference/templateLiteralTypesPatterns.js +++ b/tests/baselines/reference/templateLiteralTypesPatterns.js @@ -208,6 +208,10 @@ function conversionTest(groupName: | "downcast" | "dataDowncast" | "editingDownc conversionTest("testDowncast"); function conversionTest2(groupName: | "downcast" | "dataDowncast" | "editingDowncast" | {} & `${string}Downcast`) {} conversionTest2("testDowncast"); +function conversionTest3(groupName: | "downcast" | "dataDowncast" | "editingDowncast" | `${string & {}}Downcast`) {} +conversionTest3("testDowncast"); +function conversionTest4(groupName: | "downcast" | "dataDowncast" | "editingDowncast" | `${{} & string}Downcast`) {} +conversionTest4("testDowncast"); function foo(str: `${`a${string}` & `${string}a`}Test`) {} foo("abaTest"); // ok @@ -367,6 +371,10 @@ function conversionTest(groupName) { } conversionTest("testDowncast"); function conversionTest2(groupName) { } conversionTest2("testDowncast"); +function conversionTest3(groupName) { } +conversionTest3("testDowncast"); +function conversionTest4(groupName) { } +conversionTest4("testDowncast"); function foo(str) { } foo("abaTest"); // ok foo("abcTest"); // error diff --git a/tests/baselines/reference/templateLiteralTypesPatterns.symbols b/tests/baselines/reference/templateLiteralTypesPatterns.symbols index fe3b75119c772..67158ff6db1cd 100644 --- a/tests/baselines/reference/templateLiteralTypesPatterns.symbols +++ b/tests/baselines/reference/templateLiteralTypesPatterns.symbols @@ -502,13 +502,27 @@ function conversionTest2(groupName: | "downcast" | "dataDowncast" | "editingDown conversionTest2("testDowncast"); >conversionTest2 : Symbol(conversionTest2, Decl(templateLiteralTypesPatterns.ts, 204, 31)) +function conversionTest3(groupName: | "downcast" | "dataDowncast" | "editingDowncast" | `${string & {}}Downcast`) {} +>conversionTest3 : Symbol(conversionTest3, Decl(templateLiteralTypesPatterns.ts, 206, 32)) +>groupName : Symbol(groupName, Decl(templateLiteralTypesPatterns.ts, 207, 25)) + +conversionTest3("testDowncast"); +>conversionTest3 : Symbol(conversionTest3, Decl(templateLiteralTypesPatterns.ts, 206, 32)) + +function conversionTest4(groupName: | "downcast" | "dataDowncast" | "editingDowncast" | `${{} & string}Downcast`) {} +>conversionTest4 : Symbol(conversionTest4, Decl(templateLiteralTypesPatterns.ts, 208, 32)) +>groupName : Symbol(groupName, Decl(templateLiteralTypesPatterns.ts, 209, 25)) + +conversionTest4("testDowncast"); +>conversionTest4 : Symbol(conversionTest4, Decl(templateLiteralTypesPatterns.ts, 208, 32)) + function foo(str: `${`a${string}` & `${string}a`}Test`) {} ->foo : Symbol(foo, Decl(templateLiteralTypesPatterns.ts, 206, 32)) ->str : Symbol(str, Decl(templateLiteralTypesPatterns.ts, 208, 13)) +>foo : Symbol(foo, Decl(templateLiteralTypesPatterns.ts, 210, 32)) +>str : Symbol(str, Decl(templateLiteralTypesPatterns.ts, 212, 13)) foo("abaTest"); // ok ->foo : Symbol(foo, Decl(templateLiteralTypesPatterns.ts, 206, 32)) +>foo : Symbol(foo, Decl(templateLiteralTypesPatterns.ts, 210, 32)) foo("abcTest"); // error ->foo : Symbol(foo, Decl(templateLiteralTypesPatterns.ts, 206, 32)) +>foo : Symbol(foo, Decl(templateLiteralTypesPatterns.ts, 210, 32)) diff --git a/tests/baselines/reference/templateLiteralTypesPatterns.types b/tests/baselines/reference/templateLiteralTypesPatterns.types index 88864ad046ba2..d63799df57018 100644 --- a/tests/baselines/reference/templateLiteralTypesPatterns.types +++ b/tests/baselines/reference/templateLiteralTypesPatterns.types @@ -654,6 +654,24 @@ conversionTest2("testDowncast"); >conversionTest2 : (groupName: "downcast" | "dataDowncast" | "editingDowncast" | ({} & `${string}Downcast`)) => void >"testDowncast" : "testDowncast" +function conversionTest3(groupName: | "downcast" | "dataDowncast" | "editingDowncast" | `${string & {}}Downcast`) {} +>conversionTest3 : (groupName: "downcast" | "dataDowncast" | "editingDowncast" | `${string & {}}Downcast`) => void +>groupName : "downcast" | `${string & {}}Downcast` + +conversionTest3("testDowncast"); +>conversionTest3("testDowncast") : void +>conversionTest3 : (groupName: "downcast" | `${string & {}}Downcast`) => void +>"testDowncast" : "testDowncast" + +function conversionTest4(groupName: | "downcast" | "dataDowncast" | "editingDowncast" | `${{} & string}Downcast`) {} +>conversionTest4 : (groupName: "downcast" | "dataDowncast" | "editingDowncast" | `${{} & string}Downcast`) => void +>groupName : "downcast" | `${{} & string}Downcast` + +conversionTest4("testDowncast"); +>conversionTest4("testDowncast") : void +>conversionTest4 : (groupName: "downcast" | `${{} & string}Downcast`) => void +>"testDowncast" : "testDowncast" + function foo(str: `${`a${string}` & `${string}a`}Test`) {} >foo : (str: `${`a${string}` & `${string}a`}Test`) => void >str : `${`a${string}` & `${string}a`}Test` diff --git a/tests/cases/conformance/types/literal/templateLiteralTypesPatterns.ts b/tests/cases/conformance/types/literal/templateLiteralTypesPatterns.ts index ada8e7f9f17ca..00d36bd722a29 100644 --- a/tests/cases/conformance/types/literal/templateLiteralTypesPatterns.ts +++ b/tests/cases/conformance/types/literal/templateLiteralTypesPatterns.ts @@ -206,6 +206,10 @@ function conversionTest(groupName: | "downcast" | "dataDowncast" | "editingDownc conversionTest("testDowncast"); function conversionTest2(groupName: | "downcast" | "dataDowncast" | "editingDowncast" | {} & `${string}Downcast`) {} conversionTest2("testDowncast"); +function conversionTest3(groupName: | "downcast" | "dataDowncast" | "editingDowncast" | `${string & {}}Downcast`) {} +conversionTest3("testDowncast"); +function conversionTest4(groupName: | "downcast" | "dataDowncast" | "editingDowncast" | `${{} & string}Downcast`) {} +conversionTest4("testDowncast"); function foo(str: `${`a${string}` & `${string}a`}Test`) {} foo("abaTest"); // ok