Skip to content

Commit e1907a2

Browse files
committed
Add tests
1 parent 7f34792 commit e1907a2

File tree

5 files changed

+339
-53
lines changed

5 files changed

+339
-53
lines changed

tests/baselines/reference/templateLiteralTypes3.errors.txt

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ tests/cases/conformance/types/literal/templateLiteralTypes3.ts(71,5): error TS23
55
tests/cases/conformance/types/literal/templateLiteralTypes3.ts(72,5): error TS2322: Type '`*${string}*`' is not assignable to type '`*${number}*`'.
66
tests/cases/conformance/types/literal/templateLiteralTypes3.ts(74,5): error TS2322: Type '"*false*" | "*true*"' is not assignable to type '`*${number}*`'.
77
Type '"*false*"' is not assignable to type '`*${number}*`'.
8+
tests/cases/conformance/types/literal/templateLiteralTypes3.ts(133,9): error TS2367: This condition will always return 'false' since the types '`foo-${string}`' and '`baz-${string}`' have no overlap.
89

910

10-
==== tests/cases/conformance/types/literal/templateLiteralTypes3.ts (6 errors) ====
11+
==== tests/cases/conformance/types/literal/templateLiteralTypes3.ts (7 errors) ====
1112
// Inference from template literal type to template literal type
1213

1314
type Foo1<T> = T extends `*${infer U}*` ? U : never;
@@ -146,4 +147,44 @@ tests/cases/conformance/types/literal/templateLiteralTypes3.ts(74,5): error TS23
146147
declare function chain<F extends keyof Schema>(field: F | `${F}.${F}`): void;
147148

148149
chain("a");
150+
151+
// Repro from #46125
152+
153+
function ff1<T extends string>(x: `foo-${string}`, y: `${string}-bar`, z: `baz-${string}`) {
154+
if (x === y) {
155+
x; // `foo-${string}`
156+
}
157+
if (x === z) { // Error
158+
~~~~~~~
159+
!!! error TS2367: This condition will always return 'false' since the types '`foo-${string}`' and '`baz-${string}`' have no overlap.
160+
}
161+
}
162+
163+
function ff2(x: string, y: `foo-${string}` | 'bar') {
164+
if (x === y) {
165+
x; // `foo-${string}` | 'bar'
166+
}
167+
}
168+
169+
function ff3(x: string, y: `foo-${string}`) {
170+
if (x === 'foo-test') {
171+
x; // 'foo-test'
172+
}
173+
if (y === 'foo-test') {
174+
y; // 'foo-test'
175+
}
176+
}
177+
178+
// Repro from #46045
179+
180+
export type Action =
181+
| { type: `${string}_REQUEST` }
182+
| { type: `${string}_SUCCESS`, response: string };
183+
184+
export function reducer(action: Action) {
185+
if (action.type === 'FOO_SUCCESS') {
186+
action.type;
187+
action.response;
188+
}
189+
}
149190

tests/baselines/reference/templateLiteralTypes3.js

Lines changed: 74 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,51 @@ type Schema = { a: { b: { c: number } } };
124124
declare function chain<F extends keyof Schema>(field: F | `${F}.${F}`): void;
125125

126126
chain("a");
127+
128+
// Repro from #46125
129+
130+
function ff1<T extends string>(x: `foo-${string}`, y: `${string}-bar`, z: `baz-${string}`) {
131+
if (x === y) {
132+
x; // `foo-${string}`
133+
}
134+
if (x === z) { // Error
135+
}
136+
}
137+
138+
function ff2(x: string, y: `foo-${string}` | 'bar') {
139+
if (x === y) {
140+
x; // `foo-${string}` | 'bar'
141+
}
142+
}
143+
144+
function ff3(x: string, y: `foo-${string}`) {
145+
if (x === 'foo-test') {
146+
x; // 'foo-test'
147+
}
148+
if (y === 'foo-test') {
149+
y; // 'foo-test'
150+
}
151+
}
152+
153+
// Repro from #46045
154+
155+
export type Action =
156+
| { type: `${string}_REQUEST` }
157+
| { type: `${string}_SUCCESS`, response: string };
158+
159+
export function reducer(action: Action) {
160+
if (action.type === 'FOO_SUCCESS') {
161+
action.type;
162+
action.response;
163+
}
164+
}
127165

128166

129167
//// [templateLiteralTypes3.js]
130168
"use strict";
131169
// Inference from template literal type to template literal type
170+
exports.__esModule = true;
171+
exports.reducer = void 0;
132172
function f1(s, n, b, t) {
133173
var x1 = foo1('hello'); // Error
134174
var x2 = foo1('*hello*');
@@ -177,59 +217,41 @@ var templated1 = value1 + " abc";
177217
var value2 = "abc";
178218
var templated2 = value2 + " abc";
179219
chain("a");
220+
// Repro from #46125
221+
function ff1(x, y, z) {
222+
if (x === y) {
223+
x; // `foo-${string}`
224+
}
225+
if (x === z) { // Error
226+
}
227+
}
228+
function ff2(x, y) {
229+
if (x === y) {
230+
x; // `foo-${string}` | 'bar'
231+
}
232+
}
233+
function ff3(x, y) {
234+
if (x === 'foo-test') {
235+
x; // 'foo-test'
236+
}
237+
if (y === 'foo-test') {
238+
y; // 'foo-test'
239+
}
240+
}
241+
function reducer(action) {
242+
if (action.type === 'FOO_SUCCESS') {
243+
action.type;
244+
action.response;
245+
}
246+
}
247+
exports.reducer = reducer;
180248

181249

182250
//// [templateLiteralTypes3.d.ts]
183-
declare type Foo1<T> = T extends `*${infer U}*` ? U : never;
184-
declare type T01 = Foo1<'hello'>;
185-
declare type T02 = Foo1<'*hello*'>;
186-
declare type T03 = Foo1<'**hello**'>;
187-
declare type T04 = Foo1<`*${string}*`>;
188-
declare type T05 = Foo1<`*${number}*`>;
189-
declare type T06 = Foo1<`*${bigint}*`>;
190-
declare type T07 = Foo1<`*${any}*`>;
191-
declare type T08 = Foo1<`**${string}**`>;
192-
declare type T09 = Foo1<`**${string}**${string}**`>;
193-
declare type T10 = Foo1<`**${'a' | 'b' | 'c'}**`>;
194-
declare type T11 = Foo1<`**${boolean}**${boolean}**`>;
195-
declare function foo1<V extends string>(arg: `*${V}*`): V;
196-
declare function f1<T extends string>(s: string, n: number, b: boolean, t: T): void;
197-
declare type Parts<T> = T extends '' ? [] : T extends `${infer Head}${infer Tail}` ? [Head, ...Parts<Tail>] : never;
198-
declare type T20 = Parts<`abc`>;
199-
declare type T21 = Parts<`*${string}*`>;
200-
declare type T22 = Parts<`*${number}*`>;
201-
declare type T23 = Parts<`*${number}*${string}*${bigint}*`>;
202-
declare function f2(): void;
203-
declare function f3<T extends string>(s: string, n: number, b: boolean, t: T): void;
204-
declare function f4<T extends number>(s: string, n: number, b: boolean, t: T): void;
205-
declare type A<T> = T extends `${infer U}.${infer V}` ? U | V : never;
206-
declare type B = A<`test.1024`>;
207-
declare type C = A<`test.${number}`>;
208-
declare type D<T> = T extends `${infer U}.${number}` ? U : never;
209-
declare type E = D<`test.1024`>;
210-
declare type F = D<`test.${number}`>;
211-
declare type G<T> = T extends `${infer U}.${infer V}` ? U | V : never;
212-
declare type H = G<`test.hoge`>;
213-
declare type I = G<`test.${string}`>;
214-
declare type J<T> = T extends `${infer U}.${string}` ? U : never;
215-
declare type K = J<`test.hoge`>;
216-
declare type L = J<`test.${string}`>;
217-
declare type Templated = `${string} ${string}`;
218-
declare const value1: string;
219-
declare const templated1: Templated;
220-
declare const value2 = "abc";
221-
declare const templated2: Templated;
222-
declare type Prefixes = "foo" | "bar";
223-
declare type AllPrefixData = "foo:baz" | "bar:baz";
224-
declare type PrefixData<P extends Prefixes> = `${P}:baz`;
225-
interface ITest<P extends Prefixes, E extends AllPrefixData = PrefixData<P>> {
226-
blah: string;
227-
}
228-
declare type Schema = {
229-
a: {
230-
b: {
231-
c: number;
232-
};
233-
};
251+
export declare type Action = {
252+
type: `${string}_REQUEST`;
253+
} | {
254+
type: `${string}_SUCCESS`;
255+
response: string;
234256
};
235-
declare function chain<F extends keyof Schema>(field: F | `${F}.${F}`): void;
257+
export declare function reducer(action: Action): void;

tests/baselines/reference/templateLiteralTypes3.symbols

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,3 +405,92 @@ declare function chain<F extends keyof Schema>(field: F | `${F}.${F}`): void;
405405
chain("a");
406406
>chain : Symbol(chain, Decl(templateLiteralTypes3.ts, 120, 42))
407407

408+
// Repro from #46125
409+
410+
function ff1<T extends string>(x: `foo-${string}`, y: `${string}-bar`, z: `baz-${string}`) {
411+
>ff1 : Symbol(ff1, Decl(templateLiteralTypes3.ts, 124, 11))
412+
>T : Symbol(T, Decl(templateLiteralTypes3.ts, 128, 13))
413+
>x : Symbol(x, Decl(templateLiteralTypes3.ts, 128, 31))
414+
>y : Symbol(y, Decl(templateLiteralTypes3.ts, 128, 50))
415+
>z : Symbol(z, Decl(templateLiteralTypes3.ts, 128, 70))
416+
417+
if (x === y) {
418+
>x : Symbol(x, Decl(templateLiteralTypes3.ts, 128, 31))
419+
>y : Symbol(y, Decl(templateLiteralTypes3.ts, 128, 50))
420+
421+
x; // `foo-${string}`
422+
>x : Symbol(x, Decl(templateLiteralTypes3.ts, 128, 31))
423+
}
424+
if (x === z) { // Error
425+
>x : Symbol(x, Decl(templateLiteralTypes3.ts, 128, 31))
426+
>z : Symbol(z, Decl(templateLiteralTypes3.ts, 128, 70))
427+
}
428+
}
429+
430+
function ff2(x: string, y: `foo-${string}` | 'bar') {
431+
>ff2 : Symbol(ff2, Decl(templateLiteralTypes3.ts, 134, 1))
432+
>x : Symbol(x, Decl(templateLiteralTypes3.ts, 136, 13))
433+
>y : Symbol(y, Decl(templateLiteralTypes3.ts, 136, 23))
434+
435+
if (x === y) {
436+
>x : Symbol(x, Decl(templateLiteralTypes3.ts, 136, 13))
437+
>y : Symbol(y, Decl(templateLiteralTypes3.ts, 136, 23))
438+
439+
x; // `foo-${string}` | 'bar'
440+
>x : Symbol(x, Decl(templateLiteralTypes3.ts, 136, 13))
441+
}
442+
}
443+
444+
function ff3(x: string, y: `foo-${string}`) {
445+
>ff3 : Symbol(ff3, Decl(templateLiteralTypes3.ts, 140, 1))
446+
>x : Symbol(x, Decl(templateLiteralTypes3.ts, 142, 13))
447+
>y : Symbol(y, Decl(templateLiteralTypes3.ts, 142, 23))
448+
449+
if (x === 'foo-test') {
450+
>x : Symbol(x, Decl(templateLiteralTypes3.ts, 142, 13))
451+
452+
x; // 'foo-test'
453+
>x : Symbol(x, Decl(templateLiteralTypes3.ts, 142, 13))
454+
}
455+
if (y === 'foo-test') {
456+
>y : Symbol(y, Decl(templateLiteralTypes3.ts, 142, 23))
457+
458+
y; // 'foo-test'
459+
>y : Symbol(y, Decl(templateLiteralTypes3.ts, 142, 23))
460+
}
461+
}
462+
463+
// Repro from #46045
464+
465+
export type Action =
466+
>Action : Symbol(Action, Decl(templateLiteralTypes3.ts, 149, 1))
467+
468+
| { type: `${string}_REQUEST` }
469+
>type : Symbol(type, Decl(templateLiteralTypes3.ts, 154, 7))
470+
471+
| { type: `${string}_SUCCESS`, response: string };
472+
>type : Symbol(type, Decl(templateLiteralTypes3.ts, 155, 7))
473+
>response : Symbol(response, Decl(templateLiteralTypes3.ts, 155, 34))
474+
475+
export function reducer(action: Action) {
476+
>reducer : Symbol(reducer, Decl(templateLiteralTypes3.ts, 155, 54))
477+
>action : Symbol(action, Decl(templateLiteralTypes3.ts, 157, 24))
478+
>Action : Symbol(Action, Decl(templateLiteralTypes3.ts, 149, 1))
479+
480+
if (action.type === 'FOO_SUCCESS') {
481+
>action.type : Symbol(type, Decl(templateLiteralTypes3.ts, 154, 7), Decl(templateLiteralTypes3.ts, 155, 7))
482+
>action : Symbol(action, Decl(templateLiteralTypes3.ts, 157, 24))
483+
>type : Symbol(type, Decl(templateLiteralTypes3.ts, 154, 7), Decl(templateLiteralTypes3.ts, 155, 7))
484+
485+
action.type;
486+
>action.type : Symbol(type, Decl(templateLiteralTypes3.ts, 155, 7))
487+
>action : Symbol(action, Decl(templateLiteralTypes3.ts, 157, 24))
488+
>type : Symbol(type, Decl(templateLiteralTypes3.ts, 155, 7))
489+
490+
action.response;
491+
>action.response : Symbol(response, Decl(templateLiteralTypes3.ts, 155, 34))
492+
>action : Symbol(action, Decl(templateLiteralTypes3.ts, 157, 24))
493+
>response : Symbol(response, Decl(templateLiteralTypes3.ts, 155, 34))
494+
}
495+
}
496+

tests/baselines/reference/templateLiteralTypes3.types

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,3 +396,99 @@ chain("a");
396396
>chain : <F extends "a">(field: F | `${F}.${F}`) => void
397397
>"a" : "a"
398398

399+
// Repro from #46125
400+
401+
function ff1<T extends string>(x: `foo-${string}`, y: `${string}-bar`, z: `baz-${string}`) {
402+
>ff1 : <T extends string>(x: `foo-${string}`, y: `${string}-bar`, z: `baz-${string}`) => void
403+
>x : `foo-${string}`
404+
>y : `${string}-bar`
405+
>z : `baz-${string}`
406+
407+
if (x === y) {
408+
>x === y : boolean
409+
>x : `foo-${string}`
410+
>y : `${string}-bar`
411+
412+
x; // `foo-${string}`
413+
>x : `foo-${string}`
414+
}
415+
if (x === z) { // Error
416+
>x === z : boolean
417+
>x : `foo-${string}`
418+
>z : `baz-${string}`
419+
}
420+
}
421+
422+
function ff2(x: string, y: `foo-${string}` | 'bar') {
423+
>ff2 : (x: string, y: `foo-${string}` | 'bar') => void
424+
>x : string
425+
>y : "bar" | `foo-${string}`
426+
427+
if (x === y) {
428+
>x === y : boolean
429+
>x : string
430+
>y : "bar" | `foo-${string}`
431+
432+
x; // `foo-${string}` | 'bar'
433+
>x : "bar" | `foo-${string}`
434+
}
435+
}
436+
437+
function ff3(x: string, y: `foo-${string}`) {
438+
>ff3 : (x: string, y: `foo-${string}`) => void
439+
>x : string
440+
>y : `foo-${string}`
441+
442+
if (x === 'foo-test') {
443+
>x === 'foo-test' : boolean
444+
>x : string
445+
>'foo-test' : "foo-test"
446+
447+
x; // 'foo-test'
448+
>x : "foo-test"
449+
}
450+
if (y === 'foo-test') {
451+
>y === 'foo-test' : boolean
452+
>y : `foo-${string}`
453+
>'foo-test' : "foo-test"
454+
455+
y; // 'foo-test'
456+
>y : "foo-test"
457+
}
458+
}
459+
460+
// Repro from #46045
461+
462+
export type Action =
463+
>Action : Action
464+
465+
| { type: `${string}_REQUEST` }
466+
>type : `${string}_REQUEST`
467+
468+
| { type: `${string}_SUCCESS`, response: string };
469+
>type : `${string}_SUCCESS`
470+
>response : string
471+
472+
export function reducer(action: Action) {
473+
>reducer : (action: Action) => void
474+
>action : Action
475+
476+
if (action.type === 'FOO_SUCCESS') {
477+
>action.type === 'FOO_SUCCESS' : boolean
478+
>action.type : `${string}_REQUEST` | `${string}_SUCCESS`
479+
>action : Action
480+
>type : `${string}_REQUEST` | `${string}_SUCCESS`
481+
>'FOO_SUCCESS' : "FOO_SUCCESS"
482+
483+
action.type;
484+
>action.type : "FOO_SUCCESS"
485+
>action : { type: `${string}_SUCCESS`; response: string; }
486+
>type : "FOO_SUCCESS"
487+
488+
action.response;
489+
>action.response : string
490+
>action : { type: `${string}_SUCCESS`; response: string; }
491+
>response : string
492+
}
493+
}
494+

0 commit comments

Comments
 (0)