diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index cccf77a3d56..d9afe5e47ec 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -20056,14 +20056,12 @@ namespace ts { } function inferToTemplateLiteralType(source: Type, target: TemplateLiteralType) { - if (source.flags & (TypeFlags.StringLike | TypeFlags.Index)) { - const matches = source.flags & TypeFlags.StringLiteral ? inferLiteralsFromTemplateLiteralType(source, target) : - source.flags & TypeFlags.TemplateLiteral && arraysEqual((source).texts, target.texts) && arraysEqual((source).casings, target.casings)? (source).types : - undefined; - const types = target.types; - for (let i = 0; i < types.length; i++) { - inferFromTypes(matches ? matches[i] : source.flags & TypeFlags.StringLiteral ? neverType : stringType, types[i]); - } + const matches = source.flags & TypeFlags.StringLiteral ? inferLiteralsFromTemplateLiteralType(source, target) : + source.flags & TypeFlags.TemplateLiteral && arraysEqual((source).texts, target.texts) && arraysEqual((source).casings, target.casings)? (source).types : + undefined; + const types = target.types; + for (let i = 0; i < types.length; i++) { + inferFromTypes(matches ? matches[i] : neverType, types[i]); } } diff --git a/tests/baselines/reference/templateLiteralTypes1.errors.txt b/tests/baselines/reference/templateLiteralTypes1.errors.txt index e4b8aa2920b..6a453496896 100644 --- a/tests/baselines/reference/templateLiteralTypes1.errors.txt +++ b/tests/baselines/reference/templateLiteralTypes1.errors.txt @@ -5,9 +5,9 @@ tests/cases/conformance/types/literal/templateLiteralTypes1.ts(39,5): error TS23 Type 'string' is not assignable to type 'B'. 'string' is assignable to the constraint of type 'B', but 'B' could be instantiated with a different subtype of constraint 'string'. tests/cases/conformance/types/literal/templateLiteralTypes1.ts(165,15): error TS1338: 'infer' declarations are only permitted in the 'extends' clause of a conditional type. -tests/cases/conformance/types/literal/templateLiteralTypes1.ts(188,16): error TS2590: Expression produces a union type that is too complex to represent. -tests/cases/conformance/types/literal/templateLiteralTypes1.ts(192,16): error TS2590: Expression produces a union type that is too complex to represent. -tests/cases/conformance/types/literal/templateLiteralTypes1.ts(196,16): error TS2590: Expression produces a union type that is too complex to represent. +tests/cases/conformance/types/literal/templateLiteralTypes1.ts(197,16): error TS2590: Expression produces a union type that is too complex to represent. +tests/cases/conformance/types/literal/templateLiteralTypes1.ts(201,16): error TS2590: Expression produces a union type that is too complex to represent. +tests/cases/conformance/types/literal/templateLiteralTypes1.ts(205,16): error TS2590: Expression produces a union type that is too complex to represent. ==== tests/cases/conformance/types/literal/templateLiteralTypes1.ts (6 errors) ==== @@ -198,6 +198,15 @@ tests/cases/conformance/types/literal/templateLiteralTypes1.ts(196,16): error TS type L1 = Chars<'FooBarBazThisIsALongerString'>; // ['F', 'o', 'o', 'B', 'a', 'r', ...] + // Infer never when source isn't a literal type that matches the pattern + + type Foo = T extends `*${infer S}*` ? S : never; + + type TF1 = Foo; // never + type TF2 = Foo; // never + type TF3 = Foo<'abc'>; // never + type TF4 = Foo<'*abc*'>; // 'abc' + // Cross product unions limited to 100,000 constituents type A = any; diff --git a/tests/baselines/reference/templateLiteralTypes1.js b/tests/baselines/reference/templateLiteralTypes1.js index fb782891d04..bc880baef8c 100644 --- a/tests/baselines/reference/templateLiteralTypes1.js +++ b/tests/baselines/reference/templateLiteralTypes1.js @@ -176,6 +176,15 @@ type Chars = type L1 = Chars<'FooBarBazThisIsALongerString'>; // ['F', 'o', 'o', 'B', 'a', 'r', ...] +// Infer never when source isn't a literal type that matches the pattern + +type Foo = T extends `*${infer S}*` ? S : never; + +type TF1 = Foo; // never +type TF2 = Foo; // never +type TF3 = Foo<'abc'>; // never +type TF4 = Foo<'*abc*'>; // 'abc' + // Cross product unions limited to 100,000 constituents type A = any; @@ -329,6 +338,11 @@ declare type S2 = S; declare type TV1 = `${infer X}`; declare type Chars = string extends S ? string[] : S extends `${infer C0}${infer C1}${infer C2}${infer C3}${infer C4}${infer C5}${infer C6}${infer C7}${infer C8}${infer C9}${infer R}` ? [C0, C1, C2, C3, C4, C5, C6, C7, C8, C9, ...Chars] : S extends `${infer C}${infer R}` ? [C, ...Chars] : S extends '' ? [] : never; declare type L1 = Chars<'FooBarBazThisIsALongerString'>; +declare type Foo = T extends `*${infer S}*` ? S : never; +declare type TF1 = Foo; +declare type TF2 = Foo; +declare type TF3 = Foo<'abc'>; +declare type TF4 = Foo<'*abc*'>; declare type A = any; declare type U1 = { a1: A; diff --git a/tests/baselines/reference/templateLiteralTypes1.symbols b/tests/baselines/reference/templateLiteralTypes1.symbols index a39c641d538..679348bcbea 100644 --- a/tests/baselines/reference/templateLiteralTypes1.symbols +++ b/tests/baselines/reference/templateLiteralTypes1.symbols @@ -667,153 +667,178 @@ type L1 = Chars<'FooBarBazThisIsALongerString'>; // ['F', 'o', 'o', 'B', 'a', ' >L1 : Symbol(L1, Decl(templateLiteralTypes1.ts, 173, 10)) >Chars : Symbol(Chars, Decl(templateLiteralTypes1.ts, 164, 24)) +// Infer never when source isn't a literal type that matches the pattern + +type Foo = T extends `*${infer S}*` ? S : never; +>Foo : Symbol(Foo, Decl(templateLiteralTypes1.ts, 175, 48)) +>T : Symbol(T, Decl(templateLiteralTypes1.ts, 179, 9)) +>T : Symbol(T, Decl(templateLiteralTypes1.ts, 179, 9)) +>S : Symbol(S, Decl(templateLiteralTypes1.ts, 179, 33)) +>S : Symbol(S, Decl(templateLiteralTypes1.ts, 179, 33)) + +type TF1 = Foo; // never +>TF1 : Symbol(TF1, Decl(templateLiteralTypes1.ts, 179, 51)) +>Foo : Symbol(Foo, Decl(templateLiteralTypes1.ts, 175, 48)) + +type TF2 = Foo; // never +>TF2 : Symbol(TF2, Decl(templateLiteralTypes1.ts, 181, 20)) +>Foo : Symbol(Foo, Decl(templateLiteralTypes1.ts, 175, 48)) + +type TF3 = Foo<'abc'>; // never +>TF3 : Symbol(TF3, Decl(templateLiteralTypes1.ts, 182, 23)) +>Foo : Symbol(Foo, Decl(templateLiteralTypes1.ts, 175, 48)) + +type TF4 = Foo<'*abc*'>; // 'abc' +>TF4 : Symbol(TF4, Decl(templateLiteralTypes1.ts, 183, 22)) +>Foo : Symbol(Foo, Decl(templateLiteralTypes1.ts, 175, 48)) + // Cross product unions limited to 100,000 constituents type A = any; ->A : Symbol(A, Decl(templateLiteralTypes1.ts, 175, 48)) +>A : Symbol(A, Decl(templateLiteralTypes1.ts, 184, 24)) type U1 = {a1:A} | {b1:A} | {c1:A} | {d1:A} | {e1:A} | {f1:A} | {g1:A} | {h1:A} | {i1:A} | {j1:A}; ->U1 : Symbol(U1, Decl(templateLiteralTypes1.ts, 179, 13)) ->a1 : Symbol(a1, Decl(templateLiteralTypes1.ts, 181, 11)) ->A : Symbol(A, Decl(templateLiteralTypes1.ts, 175, 48)) ->b1 : Symbol(b1, Decl(templateLiteralTypes1.ts, 181, 20)) ->A : Symbol(A, Decl(templateLiteralTypes1.ts, 175, 48)) ->c1 : Symbol(c1, Decl(templateLiteralTypes1.ts, 181, 29)) ->A : Symbol(A, Decl(templateLiteralTypes1.ts, 175, 48)) ->d1 : Symbol(d1, Decl(templateLiteralTypes1.ts, 181, 38)) ->A : Symbol(A, Decl(templateLiteralTypes1.ts, 175, 48)) ->e1 : Symbol(e1, Decl(templateLiteralTypes1.ts, 181, 47)) ->A : Symbol(A, Decl(templateLiteralTypes1.ts, 175, 48)) ->f1 : Symbol(f1, Decl(templateLiteralTypes1.ts, 181, 56)) ->A : Symbol(A, Decl(templateLiteralTypes1.ts, 175, 48)) ->g1 : Symbol(g1, Decl(templateLiteralTypes1.ts, 181, 65)) ->A : Symbol(A, Decl(templateLiteralTypes1.ts, 175, 48)) ->h1 : Symbol(h1, Decl(templateLiteralTypes1.ts, 181, 74)) ->A : Symbol(A, Decl(templateLiteralTypes1.ts, 175, 48)) ->i1 : Symbol(i1, Decl(templateLiteralTypes1.ts, 181, 83)) ->A : Symbol(A, Decl(templateLiteralTypes1.ts, 175, 48)) ->j1 : Symbol(j1, Decl(templateLiteralTypes1.ts, 181, 92)) ->A : Symbol(A, Decl(templateLiteralTypes1.ts, 175, 48)) +>U1 : Symbol(U1, Decl(templateLiteralTypes1.ts, 188, 13)) +>a1 : Symbol(a1, Decl(templateLiteralTypes1.ts, 190, 11)) +>A : Symbol(A, Decl(templateLiteralTypes1.ts, 184, 24)) +>b1 : Symbol(b1, Decl(templateLiteralTypes1.ts, 190, 20)) +>A : Symbol(A, Decl(templateLiteralTypes1.ts, 184, 24)) +>c1 : Symbol(c1, Decl(templateLiteralTypes1.ts, 190, 29)) +>A : Symbol(A, Decl(templateLiteralTypes1.ts, 184, 24)) +>d1 : Symbol(d1, Decl(templateLiteralTypes1.ts, 190, 38)) +>A : Symbol(A, Decl(templateLiteralTypes1.ts, 184, 24)) +>e1 : Symbol(e1, Decl(templateLiteralTypes1.ts, 190, 47)) +>A : Symbol(A, Decl(templateLiteralTypes1.ts, 184, 24)) +>f1 : Symbol(f1, Decl(templateLiteralTypes1.ts, 190, 56)) +>A : Symbol(A, Decl(templateLiteralTypes1.ts, 184, 24)) +>g1 : Symbol(g1, Decl(templateLiteralTypes1.ts, 190, 65)) +>A : Symbol(A, Decl(templateLiteralTypes1.ts, 184, 24)) +>h1 : Symbol(h1, Decl(templateLiteralTypes1.ts, 190, 74)) +>A : Symbol(A, Decl(templateLiteralTypes1.ts, 184, 24)) +>i1 : Symbol(i1, Decl(templateLiteralTypes1.ts, 190, 83)) +>A : Symbol(A, Decl(templateLiteralTypes1.ts, 184, 24)) +>j1 : Symbol(j1, Decl(templateLiteralTypes1.ts, 190, 92)) +>A : Symbol(A, Decl(templateLiteralTypes1.ts, 184, 24)) type U2 = {a2:A} | {b2:A} | {c2:A} | {d2:A} | {e2:A} | {f2:A} | {g2:A} | {h2:A} | {i2:A} | {j2:A}; ->U2 : Symbol(U2, Decl(templateLiteralTypes1.ts, 181, 98)) ->a2 : Symbol(a2, Decl(templateLiteralTypes1.ts, 182, 11)) ->A : Symbol(A, Decl(templateLiteralTypes1.ts, 175, 48)) ->b2 : Symbol(b2, Decl(templateLiteralTypes1.ts, 182, 20)) ->A : Symbol(A, Decl(templateLiteralTypes1.ts, 175, 48)) ->c2 : Symbol(c2, Decl(templateLiteralTypes1.ts, 182, 29)) ->A : Symbol(A, Decl(templateLiteralTypes1.ts, 175, 48)) ->d2 : Symbol(d2, Decl(templateLiteralTypes1.ts, 182, 38)) ->A : Symbol(A, Decl(templateLiteralTypes1.ts, 175, 48)) ->e2 : Symbol(e2, Decl(templateLiteralTypes1.ts, 182, 47)) ->A : Symbol(A, Decl(templateLiteralTypes1.ts, 175, 48)) ->f2 : Symbol(f2, Decl(templateLiteralTypes1.ts, 182, 56)) ->A : Symbol(A, Decl(templateLiteralTypes1.ts, 175, 48)) ->g2 : Symbol(g2, Decl(templateLiteralTypes1.ts, 182, 65)) ->A : Symbol(A, Decl(templateLiteralTypes1.ts, 175, 48)) ->h2 : Symbol(h2, Decl(templateLiteralTypes1.ts, 182, 74)) ->A : Symbol(A, Decl(templateLiteralTypes1.ts, 175, 48)) ->i2 : Symbol(i2, Decl(templateLiteralTypes1.ts, 182, 83)) ->A : Symbol(A, Decl(templateLiteralTypes1.ts, 175, 48)) ->j2 : Symbol(j2, Decl(templateLiteralTypes1.ts, 182, 92)) ->A : Symbol(A, Decl(templateLiteralTypes1.ts, 175, 48)) +>U2 : Symbol(U2, Decl(templateLiteralTypes1.ts, 190, 98)) +>a2 : Symbol(a2, Decl(templateLiteralTypes1.ts, 191, 11)) +>A : Symbol(A, Decl(templateLiteralTypes1.ts, 184, 24)) +>b2 : Symbol(b2, Decl(templateLiteralTypes1.ts, 191, 20)) +>A : Symbol(A, Decl(templateLiteralTypes1.ts, 184, 24)) +>c2 : Symbol(c2, Decl(templateLiteralTypes1.ts, 191, 29)) +>A : Symbol(A, Decl(templateLiteralTypes1.ts, 184, 24)) +>d2 : Symbol(d2, Decl(templateLiteralTypes1.ts, 191, 38)) +>A : Symbol(A, Decl(templateLiteralTypes1.ts, 184, 24)) +>e2 : Symbol(e2, Decl(templateLiteralTypes1.ts, 191, 47)) +>A : Symbol(A, Decl(templateLiteralTypes1.ts, 184, 24)) +>f2 : Symbol(f2, Decl(templateLiteralTypes1.ts, 191, 56)) +>A : Symbol(A, Decl(templateLiteralTypes1.ts, 184, 24)) +>g2 : Symbol(g2, Decl(templateLiteralTypes1.ts, 191, 65)) +>A : Symbol(A, Decl(templateLiteralTypes1.ts, 184, 24)) +>h2 : Symbol(h2, Decl(templateLiteralTypes1.ts, 191, 74)) +>A : Symbol(A, Decl(templateLiteralTypes1.ts, 184, 24)) +>i2 : Symbol(i2, Decl(templateLiteralTypes1.ts, 191, 83)) +>A : Symbol(A, Decl(templateLiteralTypes1.ts, 184, 24)) +>j2 : Symbol(j2, Decl(templateLiteralTypes1.ts, 191, 92)) +>A : Symbol(A, Decl(templateLiteralTypes1.ts, 184, 24)) type U3 = {a3:A} | {b3:A} | {c3:A} | {d3:A} | {e3:A} | {f3:A} | {g3:A} | {h3:A} | {i3:A} | {j3:A}; ->U3 : Symbol(U3, Decl(templateLiteralTypes1.ts, 182, 98)) ->a3 : Symbol(a3, Decl(templateLiteralTypes1.ts, 183, 11)) ->A : Symbol(A, Decl(templateLiteralTypes1.ts, 175, 48)) ->b3 : Symbol(b3, Decl(templateLiteralTypes1.ts, 183, 20)) ->A : Symbol(A, Decl(templateLiteralTypes1.ts, 175, 48)) ->c3 : Symbol(c3, Decl(templateLiteralTypes1.ts, 183, 29)) ->A : Symbol(A, Decl(templateLiteralTypes1.ts, 175, 48)) ->d3 : Symbol(d3, Decl(templateLiteralTypes1.ts, 183, 38)) ->A : Symbol(A, Decl(templateLiteralTypes1.ts, 175, 48)) ->e3 : Symbol(e3, Decl(templateLiteralTypes1.ts, 183, 47)) ->A : Symbol(A, Decl(templateLiteralTypes1.ts, 175, 48)) ->f3 : Symbol(f3, Decl(templateLiteralTypes1.ts, 183, 56)) ->A : Symbol(A, Decl(templateLiteralTypes1.ts, 175, 48)) ->g3 : Symbol(g3, Decl(templateLiteralTypes1.ts, 183, 65)) ->A : Symbol(A, Decl(templateLiteralTypes1.ts, 175, 48)) ->h3 : Symbol(h3, Decl(templateLiteralTypes1.ts, 183, 74)) ->A : Symbol(A, Decl(templateLiteralTypes1.ts, 175, 48)) ->i3 : Symbol(i3, Decl(templateLiteralTypes1.ts, 183, 83)) ->A : Symbol(A, Decl(templateLiteralTypes1.ts, 175, 48)) ->j3 : Symbol(j3, Decl(templateLiteralTypes1.ts, 183, 92)) ->A : Symbol(A, Decl(templateLiteralTypes1.ts, 175, 48)) +>U3 : Symbol(U3, Decl(templateLiteralTypes1.ts, 191, 98)) +>a3 : Symbol(a3, Decl(templateLiteralTypes1.ts, 192, 11)) +>A : Symbol(A, Decl(templateLiteralTypes1.ts, 184, 24)) +>b3 : Symbol(b3, Decl(templateLiteralTypes1.ts, 192, 20)) +>A : Symbol(A, Decl(templateLiteralTypes1.ts, 184, 24)) +>c3 : Symbol(c3, Decl(templateLiteralTypes1.ts, 192, 29)) +>A : Symbol(A, Decl(templateLiteralTypes1.ts, 184, 24)) +>d3 : Symbol(d3, Decl(templateLiteralTypes1.ts, 192, 38)) +>A : Symbol(A, Decl(templateLiteralTypes1.ts, 184, 24)) +>e3 : Symbol(e3, Decl(templateLiteralTypes1.ts, 192, 47)) +>A : Symbol(A, Decl(templateLiteralTypes1.ts, 184, 24)) +>f3 : Symbol(f3, Decl(templateLiteralTypes1.ts, 192, 56)) +>A : Symbol(A, Decl(templateLiteralTypes1.ts, 184, 24)) +>g3 : Symbol(g3, Decl(templateLiteralTypes1.ts, 192, 65)) +>A : Symbol(A, Decl(templateLiteralTypes1.ts, 184, 24)) +>h3 : Symbol(h3, Decl(templateLiteralTypes1.ts, 192, 74)) +>A : Symbol(A, Decl(templateLiteralTypes1.ts, 184, 24)) +>i3 : Symbol(i3, Decl(templateLiteralTypes1.ts, 192, 83)) +>A : Symbol(A, Decl(templateLiteralTypes1.ts, 184, 24)) +>j3 : Symbol(j3, Decl(templateLiteralTypes1.ts, 192, 92)) +>A : Symbol(A, Decl(templateLiteralTypes1.ts, 184, 24)) type U4 = {a4:A} | {b4:A} | {c4:A} | {d4:A} | {e4:A} | {f4:A} | {g4:A} | {h4:A} | {i4:A} | {j4:A}; ->U4 : Symbol(U4, Decl(templateLiteralTypes1.ts, 183, 98)) ->a4 : Symbol(a4, Decl(templateLiteralTypes1.ts, 184, 11)) ->A : Symbol(A, Decl(templateLiteralTypes1.ts, 175, 48)) ->b4 : Symbol(b4, Decl(templateLiteralTypes1.ts, 184, 20)) ->A : Symbol(A, Decl(templateLiteralTypes1.ts, 175, 48)) ->c4 : Symbol(c4, Decl(templateLiteralTypes1.ts, 184, 29)) ->A : Symbol(A, Decl(templateLiteralTypes1.ts, 175, 48)) ->d4 : Symbol(d4, Decl(templateLiteralTypes1.ts, 184, 38)) ->A : Symbol(A, Decl(templateLiteralTypes1.ts, 175, 48)) ->e4 : Symbol(e4, Decl(templateLiteralTypes1.ts, 184, 47)) ->A : Symbol(A, Decl(templateLiteralTypes1.ts, 175, 48)) ->f4 : Symbol(f4, Decl(templateLiteralTypes1.ts, 184, 56)) ->A : Symbol(A, Decl(templateLiteralTypes1.ts, 175, 48)) ->g4 : Symbol(g4, Decl(templateLiteralTypes1.ts, 184, 65)) ->A : Symbol(A, Decl(templateLiteralTypes1.ts, 175, 48)) ->h4 : Symbol(h4, Decl(templateLiteralTypes1.ts, 184, 74)) ->A : Symbol(A, Decl(templateLiteralTypes1.ts, 175, 48)) ->i4 : Symbol(i4, Decl(templateLiteralTypes1.ts, 184, 83)) ->A : Symbol(A, Decl(templateLiteralTypes1.ts, 175, 48)) ->j4 : Symbol(j4, Decl(templateLiteralTypes1.ts, 184, 92)) ->A : Symbol(A, Decl(templateLiteralTypes1.ts, 175, 48)) +>U4 : Symbol(U4, Decl(templateLiteralTypes1.ts, 192, 98)) +>a4 : Symbol(a4, Decl(templateLiteralTypes1.ts, 193, 11)) +>A : Symbol(A, Decl(templateLiteralTypes1.ts, 184, 24)) +>b4 : Symbol(b4, Decl(templateLiteralTypes1.ts, 193, 20)) +>A : Symbol(A, Decl(templateLiteralTypes1.ts, 184, 24)) +>c4 : Symbol(c4, Decl(templateLiteralTypes1.ts, 193, 29)) +>A : Symbol(A, Decl(templateLiteralTypes1.ts, 184, 24)) +>d4 : Symbol(d4, Decl(templateLiteralTypes1.ts, 193, 38)) +>A : Symbol(A, Decl(templateLiteralTypes1.ts, 184, 24)) +>e4 : Symbol(e4, Decl(templateLiteralTypes1.ts, 193, 47)) +>A : Symbol(A, Decl(templateLiteralTypes1.ts, 184, 24)) +>f4 : Symbol(f4, Decl(templateLiteralTypes1.ts, 193, 56)) +>A : Symbol(A, Decl(templateLiteralTypes1.ts, 184, 24)) +>g4 : Symbol(g4, Decl(templateLiteralTypes1.ts, 193, 65)) +>A : Symbol(A, Decl(templateLiteralTypes1.ts, 184, 24)) +>h4 : Symbol(h4, Decl(templateLiteralTypes1.ts, 193, 74)) +>A : Symbol(A, Decl(templateLiteralTypes1.ts, 184, 24)) +>i4 : Symbol(i4, Decl(templateLiteralTypes1.ts, 193, 83)) +>A : Symbol(A, Decl(templateLiteralTypes1.ts, 184, 24)) +>j4 : Symbol(j4, Decl(templateLiteralTypes1.ts, 193, 92)) +>A : Symbol(A, Decl(templateLiteralTypes1.ts, 184, 24)) type U5 = {a5:A} | {b5:A} | {c5:A} | {d5:A} | {e5:A} | {f5:A} | {g5:A} | {h5:A} | {i5:A} | {j5:A}; ->U5 : Symbol(U5, Decl(templateLiteralTypes1.ts, 184, 98)) ->a5 : Symbol(a5, Decl(templateLiteralTypes1.ts, 185, 11)) ->A : Symbol(A, Decl(templateLiteralTypes1.ts, 175, 48)) ->b5 : Symbol(b5, Decl(templateLiteralTypes1.ts, 185, 20)) ->A : Symbol(A, Decl(templateLiteralTypes1.ts, 175, 48)) ->c5 : Symbol(c5, Decl(templateLiteralTypes1.ts, 185, 29)) ->A : Symbol(A, Decl(templateLiteralTypes1.ts, 175, 48)) ->d5 : Symbol(d5, Decl(templateLiteralTypes1.ts, 185, 38)) ->A : Symbol(A, Decl(templateLiteralTypes1.ts, 175, 48)) ->e5 : Symbol(e5, Decl(templateLiteralTypes1.ts, 185, 47)) ->A : Symbol(A, Decl(templateLiteralTypes1.ts, 175, 48)) ->f5 : Symbol(f5, Decl(templateLiteralTypes1.ts, 185, 56)) ->A : Symbol(A, Decl(templateLiteralTypes1.ts, 175, 48)) ->g5 : Symbol(g5, Decl(templateLiteralTypes1.ts, 185, 65)) ->A : Symbol(A, Decl(templateLiteralTypes1.ts, 175, 48)) ->h5 : Symbol(h5, Decl(templateLiteralTypes1.ts, 185, 74)) ->A : Symbol(A, Decl(templateLiteralTypes1.ts, 175, 48)) ->i5 : Symbol(i5, Decl(templateLiteralTypes1.ts, 185, 83)) ->A : Symbol(A, Decl(templateLiteralTypes1.ts, 175, 48)) ->j5 : Symbol(j5, Decl(templateLiteralTypes1.ts, 185, 92)) ->A : Symbol(A, Decl(templateLiteralTypes1.ts, 175, 48)) +>U5 : Symbol(U5, Decl(templateLiteralTypes1.ts, 193, 98)) +>a5 : Symbol(a5, Decl(templateLiteralTypes1.ts, 194, 11)) +>A : Symbol(A, Decl(templateLiteralTypes1.ts, 184, 24)) +>b5 : Symbol(b5, Decl(templateLiteralTypes1.ts, 194, 20)) +>A : Symbol(A, Decl(templateLiteralTypes1.ts, 184, 24)) +>c5 : Symbol(c5, Decl(templateLiteralTypes1.ts, 194, 29)) +>A : Symbol(A, Decl(templateLiteralTypes1.ts, 184, 24)) +>d5 : Symbol(d5, Decl(templateLiteralTypes1.ts, 194, 38)) +>A : Symbol(A, Decl(templateLiteralTypes1.ts, 184, 24)) +>e5 : Symbol(e5, Decl(templateLiteralTypes1.ts, 194, 47)) +>A : Symbol(A, Decl(templateLiteralTypes1.ts, 184, 24)) +>f5 : Symbol(f5, Decl(templateLiteralTypes1.ts, 194, 56)) +>A : Symbol(A, Decl(templateLiteralTypes1.ts, 184, 24)) +>g5 : Symbol(g5, Decl(templateLiteralTypes1.ts, 194, 65)) +>A : Symbol(A, Decl(templateLiteralTypes1.ts, 184, 24)) +>h5 : Symbol(h5, Decl(templateLiteralTypes1.ts, 194, 74)) +>A : Symbol(A, Decl(templateLiteralTypes1.ts, 184, 24)) +>i5 : Symbol(i5, Decl(templateLiteralTypes1.ts, 194, 83)) +>A : Symbol(A, Decl(templateLiteralTypes1.ts, 184, 24)) +>j5 : Symbol(j5, Decl(templateLiteralTypes1.ts, 194, 92)) +>A : Symbol(A, Decl(templateLiteralTypes1.ts, 184, 24)) type U100000 = U1 & U2 & U3 & U4 & U5; // Error ->U100000 : Symbol(U100000, Decl(templateLiteralTypes1.ts, 185, 98)) ->U1 : Symbol(U1, Decl(templateLiteralTypes1.ts, 179, 13)) ->U2 : Symbol(U2, Decl(templateLiteralTypes1.ts, 181, 98)) ->U3 : Symbol(U3, Decl(templateLiteralTypes1.ts, 182, 98)) ->U4 : Symbol(U4, Decl(templateLiteralTypes1.ts, 183, 98)) ->U5 : Symbol(U5, Decl(templateLiteralTypes1.ts, 184, 98)) +>U100000 : Symbol(U100000, Decl(templateLiteralTypes1.ts, 194, 98)) +>U1 : Symbol(U1, Decl(templateLiteralTypes1.ts, 188, 13)) +>U2 : Symbol(U2, Decl(templateLiteralTypes1.ts, 190, 98)) +>U3 : Symbol(U3, Decl(templateLiteralTypes1.ts, 191, 98)) +>U4 : Symbol(U4, Decl(templateLiteralTypes1.ts, 192, 98)) +>U5 : Symbol(U5, Decl(templateLiteralTypes1.ts, 193, 98)) type Digits = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9; ->Digits : Symbol(Digits, Decl(templateLiteralTypes1.ts, 187, 38)) +>Digits : Symbol(Digits, Decl(templateLiteralTypes1.ts, 196, 38)) type D100000 = `${Digits}${Digits}${Digits}${Digits}${Digits}`; // Error ->D100000 : Symbol(D100000, Decl(templateLiteralTypes1.ts, 189, 52)) ->Digits : Symbol(Digits, Decl(templateLiteralTypes1.ts, 187, 38)) ->Digits : Symbol(Digits, Decl(templateLiteralTypes1.ts, 187, 38)) ->Digits : Symbol(Digits, Decl(templateLiteralTypes1.ts, 187, 38)) ->Digits : Symbol(Digits, Decl(templateLiteralTypes1.ts, 187, 38)) ->Digits : Symbol(Digits, Decl(templateLiteralTypes1.ts, 187, 38)) +>D100000 : Symbol(D100000, Decl(templateLiteralTypes1.ts, 198, 52)) +>Digits : Symbol(Digits, Decl(templateLiteralTypes1.ts, 196, 38)) +>Digits : Symbol(Digits, Decl(templateLiteralTypes1.ts, 196, 38)) +>Digits : Symbol(Digits, Decl(templateLiteralTypes1.ts, 196, 38)) +>Digits : Symbol(Digits, Decl(templateLiteralTypes1.ts, 196, 38)) +>Digits : Symbol(Digits, Decl(templateLiteralTypes1.ts, 196, 38)) type TDigits = [0] | [1] | [2] | [3] | [4] | [5] | [6] | [7] | [8] | [9]; ->TDigits : Symbol(TDigits, Decl(templateLiteralTypes1.ts, 191, 63)) +>TDigits : Symbol(TDigits, Decl(templateLiteralTypes1.ts, 200, 63)) type T100000 = [...TDigits, ...TDigits, ...TDigits, ...TDigits, ...TDigits]; // Error ->T100000 : Symbol(T100000, Decl(templateLiteralTypes1.ts, 193, 73)) ->TDigits : Symbol(TDigits, Decl(templateLiteralTypes1.ts, 191, 63)) ->TDigits : Symbol(TDigits, Decl(templateLiteralTypes1.ts, 191, 63)) ->TDigits : Symbol(TDigits, Decl(templateLiteralTypes1.ts, 191, 63)) ->TDigits : Symbol(TDigits, Decl(templateLiteralTypes1.ts, 191, 63)) ->TDigits : Symbol(TDigits, Decl(templateLiteralTypes1.ts, 191, 63)) +>T100000 : Symbol(T100000, Decl(templateLiteralTypes1.ts, 202, 73)) +>TDigits : Symbol(TDigits, Decl(templateLiteralTypes1.ts, 200, 63)) +>TDigits : Symbol(TDigits, Decl(templateLiteralTypes1.ts, 200, 63)) +>TDigits : Symbol(TDigits, Decl(templateLiteralTypes1.ts, 200, 63)) +>TDigits : Symbol(TDigits, Decl(templateLiteralTypes1.ts, 200, 63)) +>TDigits : Symbol(TDigits, Decl(templateLiteralTypes1.ts, 200, 63)) diff --git a/tests/baselines/reference/templateLiteralTypes1.types b/tests/baselines/reference/templateLiteralTypes1.types index d8b55f4971c..b34eecce1fc 100644 --- a/tests/baselines/reference/templateLiteralTypes1.types +++ b/tests/baselines/reference/templateLiteralTypes1.types @@ -414,6 +414,23 @@ type Chars = type L1 = Chars<'FooBarBazThisIsALongerString'>; // ['F', 'o', 'o', 'B', 'a', 'r', ...] >L1 : ["F", "o", "o", "B", "a", "r", "B", "a", "z", "T", "h", "i", "s", "I", "s", "A", "L", "o", "n", "g", "e", "r", "S", "t", "r", "i", "n", "g"] +// Infer never when source isn't a literal type that matches the pattern + +type Foo = T extends `*${infer S}*` ? S : never; +>Foo : Foo + +type TF1 = Foo; // never +>TF1 : never + +type TF2 = Foo; // never +>TF2 : never + +type TF3 = Foo<'abc'>; // never +>TF3 : never + +type TF4 = Foo<'*abc*'>; // 'abc' +>TF4 : "abc" + // Cross product unions limited to 100,000 constituents type A = any; diff --git a/tests/cases/conformance/types/literal/templateLiteralTypes1.ts b/tests/cases/conformance/types/literal/templateLiteralTypes1.ts index 1cd0b065343..a71badcc6ef 100644 --- a/tests/cases/conformance/types/literal/templateLiteralTypes1.ts +++ b/tests/cases/conformance/types/literal/templateLiteralTypes1.ts @@ -178,6 +178,15 @@ type Chars = type L1 = Chars<'FooBarBazThisIsALongerString'>; // ['F', 'o', 'o', 'B', 'a', 'r', ...] +// Infer never when source isn't a literal type that matches the pattern + +type Foo = T extends `*${infer S}*` ? S : never; + +type TF1 = Foo; // never +type TF2 = Foo; // never +type TF3 = Foo<'abc'>; // never +type TF4 = Foo<'*abc*'>; // 'abc' + // Cross product unions limited to 100,000 constituents type A = any;