Skip to content

Commit 950dad9

Browse files
authored
Propagate wildcard types in template literal type construction (#40875)
* Propagate wildcard types in template literal type construction * Add regression test * Accept new baselines
1 parent b93da62 commit 950dad9

File tree

6 files changed

+83
-0
lines changed

6 files changed

+83
-0
lines changed

src/compiler/checker.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13565,6 +13565,9 @@ namespace ts {
1356513565
mapType(types[unionIndex], t => getTemplateLiteralType(texts, replaceElement(types, unionIndex, t))) :
1356613566
errorType;
1356713567
}
13568+
if (contains(types, wildcardType)) {
13569+
return wildcardType;
13570+
}
1356813571
const newTypes: Type[] = [];
1356913572
const newTexts: string[] = [];
1357013573
let text = texts[0];

tests/baselines/reference/templateLiteralTypes1.errors.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,4 +232,13 @@ tests/cases/conformance/types/literal/templateLiteralTypes1.ts(205,16): error TS
232232
type T100000 = [...TDigits, ...TDigits, ...TDigits, ...TDigits, ...TDigits]; // Error
233233
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
234234
!!! error TS2590: Expression produces a union type that is too complex to represent.
235+
236+
// Repro from #40863
237+
238+
type IsNegative<T extends number> = `${T}` extends `-${string}` ? true : false;
239+
240+
type AA<T extends number, Q extends number> =
241+
[true, true] extends [IsNegative<T>, IsNegative<Q>] ? 'Every thing is ok!' : ['strange', IsNegative<T>, IsNegative<Q>];
242+
243+
type BB = AA<-2, -2>;
235244

tests/baselines/reference/templateLiteralTypes1.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,15 @@ type D100000 = `${Digits}${Digits}${Digits}${Digits}${Digits}`; // Error
204204
type TDigits = [0] | [1] | [2] | [3] | [4] | [5] | [6] | [7] | [8] | [9];
205205

206206
type T100000 = [...TDigits, ...TDigits, ...TDigits, ...TDigits, ...TDigits]; // Error
207+
208+
// Repro from #40863
209+
210+
type IsNegative<T extends number> = `${T}` extends `-${string}` ? true : false;
211+
212+
type AA<T extends number, Q extends number> =
213+
[true, true] extends [IsNegative<T>, IsNegative<Q>] ? 'Every thing is ok!' : ['strange', IsNegative<T>, IsNegative<Q>];
214+
215+
type BB = AA<-2, -2>;
207216

208217

209218
//// [templateLiteralTypes1.js]
@@ -453,3 +462,9 @@ declare type Digits = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9;
453462
declare type D100000 = `${Digits}${Digits}${Digits}${Digits}${Digits}`;
454463
declare type TDigits = [0] | [1] | [2] | [3] | [4] | [5] | [6] | [7] | [8] | [9];
455464
declare type T100000 = [...TDigits, ...TDigits, ...TDigits, ...TDigits, ...TDigits];
465+
declare type IsNegative<T extends number> = `${T}` extends `-${string}` ? true : false;
466+
declare type AA<T extends number, Q extends number> = [
467+
true,
468+
true
469+
] extends [IsNegative<T>, IsNegative<Q>] ? 'Every thing is ok!' : ['strange', IsNegative<T>, IsNegative<Q>];
470+
declare type BB = AA<-2, -2>;

tests/baselines/reference/templateLiteralTypes1.symbols

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -843,3 +843,29 @@ type T100000 = [...TDigits, ...TDigits, ...TDigits, ...TDigits, ...TDigits]; //
843843
>TDigits : Symbol(TDigits, Decl(templateLiteralTypes1.ts, 200, 63))
844844
>TDigits : Symbol(TDigits, Decl(templateLiteralTypes1.ts, 200, 63))
845845

846+
// Repro from #40863
847+
848+
type IsNegative<T extends number> = `${T}` extends `-${string}` ? true : false;
849+
>IsNegative : Symbol(IsNegative, Decl(templateLiteralTypes1.ts, 204, 76))
850+
>T : Symbol(T, Decl(templateLiteralTypes1.ts, 208, 16))
851+
>T : Symbol(T, Decl(templateLiteralTypes1.ts, 208, 16))
852+
853+
type AA<T extends number, Q extends number> =
854+
>AA : Symbol(AA, Decl(templateLiteralTypes1.ts, 208, 79))
855+
>T : Symbol(T, Decl(templateLiteralTypes1.ts, 210, 8))
856+
>Q : Symbol(Q, Decl(templateLiteralTypes1.ts, 210, 25))
857+
858+
[true, true] extends [IsNegative<T>, IsNegative<Q>] ? 'Every thing is ok!' : ['strange', IsNegative<T>, IsNegative<Q>];
859+
>IsNegative : Symbol(IsNegative, Decl(templateLiteralTypes1.ts, 204, 76))
860+
>T : Symbol(T, Decl(templateLiteralTypes1.ts, 210, 8))
861+
>IsNegative : Symbol(IsNegative, Decl(templateLiteralTypes1.ts, 204, 76))
862+
>Q : Symbol(Q, Decl(templateLiteralTypes1.ts, 210, 25))
863+
>IsNegative : Symbol(IsNegative, Decl(templateLiteralTypes1.ts, 204, 76))
864+
>T : Symbol(T, Decl(templateLiteralTypes1.ts, 210, 8))
865+
>IsNegative : Symbol(IsNegative, Decl(templateLiteralTypes1.ts, 204, 76))
866+
>Q : Symbol(Q, Decl(templateLiteralTypes1.ts, 210, 25))
867+
868+
type BB = AA<-2, -2>;
869+
>BB : Symbol(BB, Decl(templateLiteralTypes1.ts, 211, 123))
870+
>AA : Symbol(AA, Decl(templateLiteralTypes1.ts, 208, 79))
871+

tests/baselines/reference/templateLiteralTypes1.types

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,3 +515,24 @@ type TDigits = [0] | [1] | [2] | [3] | [4] | [5] | [6] | [7] | [8] | [9];
515515
type T100000 = [...TDigits, ...TDigits, ...TDigits, ...TDigits, ...TDigits]; // Error
516516
>T100000 : any
517517

518+
// Repro from #40863
519+
520+
type IsNegative<T extends number> = `${T}` extends `-${string}` ? true : false;
521+
>IsNegative : IsNegative<T>
522+
>true : true
523+
>false : false
524+
525+
type AA<T extends number, Q extends number> =
526+
>AA : AA<T, Q>
527+
528+
[true, true] extends [IsNegative<T>, IsNegative<Q>] ? 'Every thing is ok!' : ['strange', IsNegative<T>, IsNegative<Q>];
529+
>true : true
530+
>true : true
531+
532+
type BB = AA<-2, -2>;
533+
>BB : "Every thing is ok!"
534+
>-2 : -2
535+
>2 : 2
536+
>-2 : -2
537+
>2 : 2
538+

tests/cases/conformance/types/literal/templateLiteralTypes1.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,3 +206,12 @@ type D100000 = `${Digits}${Digits}${Digits}${Digits}${Digits}`; // Error
206206
type TDigits = [0] | [1] | [2] | [3] | [4] | [5] | [6] | [7] | [8] | [9];
207207

208208
type T100000 = [...TDigits, ...TDigits, ...TDigits, ...TDigits, ...TDigits]; // Error
209+
210+
// Repro from #40863
211+
212+
type IsNegative<T extends number> = `${T}` extends `-${string}` ? true : false;
213+
214+
type AA<T extends number, Q extends number> =
215+
[true, true] extends [IsNegative<T>, IsNegative<Q>] ? 'Every thing is ok!' : ['strange', IsNegative<T>, IsNegative<Q>];
216+
217+
type BB = AA<-2, -2>;

0 commit comments

Comments
 (0)