Skip to content

Commit a155082

Browse files
committed
Add a few more tests
1 parent c8ce5c6 commit a155082

File tree

5 files changed

+123
-36
lines changed

5 files changed

+123
-36
lines changed

tests/baselines/reference/templateLiteralTypes2.errors.txt

+8
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,14 @@ tests/cases/conformance/types/literal/templateLiteralTypes2.ts(70,9): error TS23
9494
!!! error TS2322: Type '`foo${number}`' is not assignable to type '{ length: number; }'.
9595
}
9696

97+
declare function g1<T>(x: T): T;
98+
declare function g2<T extends string>(x: T): T;
99+
100+
function ft20(s: string) {
101+
let x1 = g1(`xyz-${s}`); // string
102+
let x2 = g2(`xyz-${s}`); // `xyz-${string}`
103+
}
104+
97105
// Repro from #41631
98106

99107
declare function takesLiteral<T extends string>(literal: T): T extends `foo.bar.${infer R}` ? R : unknown;

tests/baselines/reference/templateLiteralTypes2.js

+15
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ function ft14(t: `foo${number}`) {
7171
let x6: { length: number } = t;
7272
}
7373

74+
declare function g1<T>(x: T): T;
75+
declare function g2<T extends string>(x: T): T;
76+
77+
function ft20(s: string) {
78+
let x1 = g1(`xyz-${s}`); // string
79+
let x2 = g2(`xyz-${s}`); // `xyz-${string}`
80+
}
81+
7482
// Repro from #41631
7583

7684
declare function takesLiteral<T extends string>(literal: T): T extends `foo.bar.${infer R}` ? R : unknown;
@@ -167,6 +175,10 @@ function ft14(t) {
167175
var x4 = t;
168176
var x6 = t;
169177
}
178+
function ft20(s) {
179+
var x1 = g1("xyz-" + s); // string
180+
var x2 = g2("xyz-" + s); // `xyz-${string}`
181+
}
170182
var t1 = takesLiteral("foo.bar.baz"); // "baz"
171183
var id2 = "foo.bar.baz";
172184
var t2 = takesLiteral(id2); // "baz"
@@ -195,6 +207,9 @@ declare function nonWidening<T extends string | number | symbol>(x: T): T;
195207
declare function ft13(s: string, cond: boolean): void;
196208
declare type T0 = string | `${number}px`;
197209
declare function ft14(t: `foo${number}`): void;
210+
declare function g1<T>(x: T): T;
211+
declare function g2<T extends string>(x: T): T;
212+
declare function ft20(s: string): void;
198213
declare function takesLiteral<T extends string>(literal: T): T extends `foo.bar.${infer R}` ? R : unknown;
199214
declare const t1: "baz";
200215
declare const id2 = "foo.bar.baz";

tests/baselines/reference/templateLiteralTypes2.symbols

+65-36
Original file line numberDiff line numberDiff line change
@@ -257,78 +257,107 @@ function ft14(t: `foo${number}`) {
257257
>t : Symbol(t, Decl(templateLiteralTypes2.ts, 64, 14))
258258
}
259259

260+
declare function g1<T>(x: T): T;
261+
>g1 : Symbol(g1, Decl(templateLiteralTypes2.ts, 70, 1))
262+
>T : Symbol(T, Decl(templateLiteralTypes2.ts, 72, 20))
263+
>x : Symbol(x, Decl(templateLiteralTypes2.ts, 72, 23))
264+
>T : Symbol(T, Decl(templateLiteralTypes2.ts, 72, 20))
265+
>T : Symbol(T, Decl(templateLiteralTypes2.ts, 72, 20))
266+
267+
declare function g2<T extends string>(x: T): T;
268+
>g2 : Symbol(g2, Decl(templateLiteralTypes2.ts, 72, 32))
269+
>T : Symbol(T, Decl(templateLiteralTypes2.ts, 73, 20))
270+
>x : Symbol(x, Decl(templateLiteralTypes2.ts, 73, 38))
271+
>T : Symbol(T, Decl(templateLiteralTypes2.ts, 73, 20))
272+
>T : Symbol(T, Decl(templateLiteralTypes2.ts, 73, 20))
273+
274+
function ft20(s: string) {
275+
>ft20 : Symbol(ft20, Decl(templateLiteralTypes2.ts, 73, 47))
276+
>s : Symbol(s, Decl(templateLiteralTypes2.ts, 75, 14))
277+
278+
let x1 = g1(`xyz-${s}`); // string
279+
>x1 : Symbol(x1, Decl(templateLiteralTypes2.ts, 76, 7))
280+
>g1 : Symbol(g1, Decl(templateLiteralTypes2.ts, 70, 1))
281+
>s : Symbol(s, Decl(templateLiteralTypes2.ts, 75, 14))
282+
283+
let x2 = g2(`xyz-${s}`); // `xyz-${string}`
284+
>x2 : Symbol(x2, Decl(templateLiteralTypes2.ts, 77, 7))
285+
>g2 : Symbol(g2, Decl(templateLiteralTypes2.ts, 72, 32))
286+
>s : Symbol(s, Decl(templateLiteralTypes2.ts, 75, 14))
287+
}
288+
260289
// Repro from #41631
261290

262291
declare function takesLiteral<T extends string>(literal: T): T extends `foo.bar.${infer R}` ? R : unknown;
263-
>takesLiteral : Symbol(takesLiteral, Decl(templateLiteralTypes2.ts, 70, 1))
264-
>T : Symbol(T, Decl(templateLiteralTypes2.ts, 74, 30))
265-
>literal : Symbol(literal, Decl(templateLiteralTypes2.ts, 74, 48))
266-
>T : Symbol(T, Decl(templateLiteralTypes2.ts, 74, 30))
267-
>T : Symbol(T, Decl(templateLiteralTypes2.ts, 74, 30))
268-
>R : Symbol(R, Decl(templateLiteralTypes2.ts, 74, 87))
269-
>R : Symbol(R, Decl(templateLiteralTypes2.ts, 74, 87))
292+
>takesLiteral : Symbol(takesLiteral, Decl(templateLiteralTypes2.ts, 78, 1))
293+
>T : Symbol(T, Decl(templateLiteralTypes2.ts, 82, 30))
294+
>literal : Symbol(literal, Decl(templateLiteralTypes2.ts, 82, 48))
295+
>T : Symbol(T, Decl(templateLiteralTypes2.ts, 82, 30))
296+
>T : Symbol(T, Decl(templateLiteralTypes2.ts, 82, 30))
297+
>R : Symbol(R, Decl(templateLiteralTypes2.ts, 82, 87))
298+
>R : Symbol(R, Decl(templateLiteralTypes2.ts, 82, 87))
270299

271300
const t1 = takesLiteral("foo.bar.baz"); // "baz"
272-
>t1 : Symbol(t1, Decl(templateLiteralTypes2.ts, 76, 5))
273-
>takesLiteral : Symbol(takesLiteral, Decl(templateLiteralTypes2.ts, 70, 1))
301+
>t1 : Symbol(t1, Decl(templateLiteralTypes2.ts, 84, 5))
302+
>takesLiteral : Symbol(takesLiteral, Decl(templateLiteralTypes2.ts, 78, 1))
274303

275304
const id2 = "foo.bar.baz";
276-
>id2 : Symbol(id2, Decl(templateLiteralTypes2.ts, 77, 5))
305+
>id2 : Symbol(id2, Decl(templateLiteralTypes2.ts, 85, 5))
277306

278307
const t2 = takesLiteral(id2); // "baz"
279-
>t2 : Symbol(t2, Decl(templateLiteralTypes2.ts, 78, 5))
280-
>takesLiteral : Symbol(takesLiteral, Decl(templateLiteralTypes2.ts, 70, 1))
281-
>id2 : Symbol(id2, Decl(templateLiteralTypes2.ts, 77, 5))
308+
>t2 : Symbol(t2, Decl(templateLiteralTypes2.ts, 86, 5))
309+
>takesLiteral : Symbol(takesLiteral, Decl(templateLiteralTypes2.ts, 78, 1))
310+
>id2 : Symbol(id2, Decl(templateLiteralTypes2.ts, 85, 5))
282311

283312
declare const someString: string;
284-
>someString : Symbol(someString, Decl(templateLiteralTypes2.ts, 80, 13))
313+
>someString : Symbol(someString, Decl(templateLiteralTypes2.ts, 88, 13))
285314

286315
const t3 = takesLiteral(`foo.bar.${someString}`); // string
287-
>t3 : Symbol(t3, Decl(templateLiteralTypes2.ts, 81, 5))
288-
>takesLiteral : Symbol(takesLiteral, Decl(templateLiteralTypes2.ts, 70, 1))
289-
>someString : Symbol(someString, Decl(templateLiteralTypes2.ts, 80, 13))
316+
>t3 : Symbol(t3, Decl(templateLiteralTypes2.ts, 89, 5))
317+
>takesLiteral : Symbol(takesLiteral, Decl(templateLiteralTypes2.ts, 78, 1))
318+
>someString : Symbol(someString, Decl(templateLiteralTypes2.ts, 88, 13))
290319

291320
const id4 = `foo.bar.${someString}`;
292-
>id4 : Symbol(id4, Decl(templateLiteralTypes2.ts, 83, 5))
293-
>someString : Symbol(someString, Decl(templateLiteralTypes2.ts, 80, 13))
321+
>id4 : Symbol(id4, Decl(templateLiteralTypes2.ts, 91, 5))
322+
>someString : Symbol(someString, Decl(templateLiteralTypes2.ts, 88, 13))
294323

295324
const t4 = takesLiteral(id4); // string
296-
>t4 : Symbol(t4, Decl(templateLiteralTypes2.ts, 84, 5))
297-
>takesLiteral : Symbol(takesLiteral, Decl(templateLiteralTypes2.ts, 70, 1))
298-
>id4 : Symbol(id4, Decl(templateLiteralTypes2.ts, 83, 5))
325+
>t4 : Symbol(t4, Decl(templateLiteralTypes2.ts, 92, 5))
326+
>takesLiteral : Symbol(takesLiteral, Decl(templateLiteralTypes2.ts, 78, 1))
327+
>id4 : Symbol(id4, Decl(templateLiteralTypes2.ts, 91, 5))
299328

300329
declare const someUnion: 'abc' | 'def' | 'ghi';
301-
>someUnion : Symbol(someUnion, Decl(templateLiteralTypes2.ts, 86, 13))
330+
>someUnion : Symbol(someUnion, Decl(templateLiteralTypes2.ts, 94, 13))
302331

303332
const t5 = takesLiteral(`foo.bar.${someUnion}`); // "abc" | "def" | "ghi"
304-
>t5 : Symbol(t5, Decl(templateLiteralTypes2.ts, 87, 5))
305-
>takesLiteral : Symbol(takesLiteral, Decl(templateLiteralTypes2.ts, 70, 1))
306-
>someUnion : Symbol(someUnion, Decl(templateLiteralTypes2.ts, 86, 13))
333+
>t5 : Symbol(t5, Decl(templateLiteralTypes2.ts, 95, 5))
334+
>takesLiteral : Symbol(takesLiteral, Decl(templateLiteralTypes2.ts, 78, 1))
335+
>someUnion : Symbol(someUnion, Decl(templateLiteralTypes2.ts, 94, 13))
307336

308337
// Repro from #41732
309338

310339
const pixelValue: number = 22;
311-
>pixelValue : Symbol(pixelValue, Decl(templateLiteralTypes2.ts, 91, 5))
340+
>pixelValue : Symbol(pixelValue, Decl(templateLiteralTypes2.ts, 99, 5))
312341

313342
type PixelValueType = `${number}px`;
314-
>PixelValueType : Symbol(PixelValueType, Decl(templateLiteralTypes2.ts, 91, 30))
343+
>PixelValueType : Symbol(PixelValueType, Decl(templateLiteralTypes2.ts, 99, 30))
315344

316345
const pixelString: PixelValueType = `22px`;
317-
>pixelString : Symbol(pixelString, Decl(templateLiteralTypes2.ts, 95, 5))
318-
>PixelValueType : Symbol(PixelValueType, Decl(templateLiteralTypes2.ts, 91, 30))
346+
>pixelString : Symbol(pixelString, Decl(templateLiteralTypes2.ts, 103, 5))
347+
>PixelValueType : Symbol(PixelValueType, Decl(templateLiteralTypes2.ts, 99, 30))
319348

320349
const pixelStringWithTemplate: PixelValueType = `${pixelValue}px`;
321-
>pixelStringWithTemplate : Symbol(pixelStringWithTemplate, Decl(templateLiteralTypes2.ts, 97, 5))
322-
>PixelValueType : Symbol(PixelValueType, Decl(templateLiteralTypes2.ts, 91, 30))
323-
>pixelValue : Symbol(pixelValue, Decl(templateLiteralTypes2.ts, 91, 5))
350+
>pixelStringWithTemplate : Symbol(pixelStringWithTemplate, Decl(templateLiteralTypes2.ts, 105, 5))
351+
>PixelValueType : Symbol(PixelValueType, Decl(templateLiteralTypes2.ts, 99, 30))
352+
>pixelValue : Symbol(pixelValue, Decl(templateLiteralTypes2.ts, 99, 5))
324353

325354
// Repro from #43143
326355

327356
function getCardTitle(title: string): `test-${string}` {
328-
>getCardTitle : Symbol(getCardTitle, Decl(templateLiteralTypes2.ts, 97, 66))
329-
>title : Symbol(title, Decl(templateLiteralTypes2.ts, 101, 22))
357+
>getCardTitle : Symbol(getCardTitle, Decl(templateLiteralTypes2.ts, 105, 66))
358+
>title : Symbol(title, Decl(templateLiteralTypes2.ts, 109, 22))
330359

331360
return `test-${title}`;
332-
>title : Symbol(title, Decl(templateLiteralTypes2.ts, 101, 22))
361+
>title : Symbol(title, Decl(templateLiteralTypes2.ts, 109, 22))
333362
}
334363

tests/baselines/reference/templateLiteralTypes2.types

+27
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,33 @@ function ft14(t: `foo${number}`) {
283283
>t : `foo${number}`
284284
}
285285

286+
declare function g1<T>(x: T): T;
287+
>g1 : <T>(x: T) => T
288+
>x : T
289+
290+
declare function g2<T extends string>(x: T): T;
291+
>g2 : <T extends string>(x: T) => T
292+
>x : T
293+
294+
function ft20(s: string) {
295+
>ft20 : (s: string) => void
296+
>s : string
297+
298+
let x1 = g1(`xyz-${s}`); // string
299+
>x1 : string
300+
>g1(`xyz-${s}`) : string
301+
>g1 : <T>(x: T) => T
302+
>`xyz-${s}` : string
303+
>s : string
304+
305+
let x2 = g2(`xyz-${s}`); // `xyz-${string}`
306+
>x2 : `xyz-${string}`
307+
>g2(`xyz-${s}`) : `xyz-${string}`
308+
>g2 : <T extends string>(x: T) => T
309+
>`xyz-${s}` : `xyz-${string}`
310+
>s : string
311+
}
312+
286313
// Repro from #41631
287314

288315
declare function takesLiteral<T extends string>(literal: T): T extends `foo.bar.${infer R}` ? R : unknown;

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

+8
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ function ft14(t: `foo${number}`) {
7373
let x6: { length: number } = t;
7474
}
7575

76+
declare function g1<T>(x: T): T;
77+
declare function g2<T extends string>(x: T): T;
78+
79+
function ft20(s: string) {
80+
let x1 = g1(`xyz-${s}`); // string
81+
let x2 = g2(`xyz-${s}`); // `xyz-${string}`
82+
}
83+
7684
// Repro from #41631
7785

7886
declare function takesLiteral<T extends string>(literal: T): T extends `foo.bar.${infer R}` ? R : unknown;

0 commit comments

Comments
 (0)