Skip to content

Commit ccf252f

Browse files
committed
Add tests
1 parent 88c7149 commit ccf252f

File tree

5 files changed

+695
-0
lines changed

5 files changed

+695
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
tests/cases/conformance/types/typeParameters/typeParameterLists/typeParameterConstModifiers.ts(40,14): error TS1277: 'const' modifier can only appear on a type parameter of a function, method or class
2+
tests/cases/conformance/types/typeParameters/typeParameterLists/typeParameterConstModifiers.ts(42,9): error TS1277: 'const' modifier can only appear on a type parameter of a function, method or class
3+
4+
5+
==== tests/cases/conformance/types/typeParameters/typeParameterLists/typeParameterConstModifiers.ts (2 errors) ====
6+
declare function f1<const T>(x: T): T;
7+
8+
const x11 = f1('a');
9+
const x12 = f1(['a', ['b', 'c']]);
10+
const x13 = f1({ a: 1, b: "c", d: ["e", 2, true, { f: "g" }] });
11+
12+
declare function f2<const T, U>(x: T | undefined): T;
13+
14+
const x21 = f2('a');
15+
const x22 = f2(['a', ['b', 'c']]);
16+
const x23 = f2({ a: 1, b: "c", d: ["e", 2, true, { f: "g" }] });
17+
18+
declare function f3<const T>(x: T): T[];
19+
20+
const x31 = f3("hello");
21+
const x32 = f3("hello");
22+
23+
declare function f4<const T>(obj: [T, T]): T;
24+
25+
const x41 = f4([[1, 'x'], [2, 'y']]);
26+
const x42 = f4([{ a: 1, b: 'x' }, { a: 2, b: 'y' }]);
27+
28+
declare function f5<const T>(obj: { x: T, y: T }): T;
29+
30+
const x51 = f5({ x: [1, 'x'], y: [2, 'y'] });
31+
const x52 = f5({ x: { a: 1, b: 'x' }, y: { a: 2, b: 'y' } });
32+
33+
declare function f6<const T extends readonly unknown[]>(...args: T): T;
34+
35+
const x61 = f6(1, 'b', { a: 1, b: 'x' });
36+
37+
class C1<const T> {
38+
constructor(x: T) {}
39+
foo<const U>(x: U) { return x; }
40+
}
41+
42+
const c71 = new C1({ a: 1, b: "c", d: ["e", 2, true, { f: "g" }] });
43+
const c72 = c71.foo(['a', ['b', 'c']]);
44+
45+
interface I1<const T> { x: T } // Error
46+
~~~~~
47+
!!! error TS1277: 'const' modifier can only appear on a type parameter of a function, method or class
48+
49+
type T1<const T> = T; // Error
50+
~~~~~
51+
!!! error TS1277: 'const' modifier can only appear on a type parameter of a function, method or class
52+
53+
// Corrected repro from #51745
54+
55+
type Obj = { a: { b: { c: "123" } } };
56+
57+
type GetPath<T, P> =
58+
P extends readonly [] ? T :
59+
P extends readonly [infer A extends keyof T, ...infer Rest] ? GetPath<T[A], Rest> :
60+
never;
61+
62+
function set<T, const P extends readonly string[]>(obj: T, path: P, value: GetPath<T, P>) {}
63+
64+
declare let obj: Obj;
65+
declare let value: "123";
66+
67+
set(obj, ['a', 'b', 'c'], value);
68+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
//// [typeParameterConstModifiers.ts]
2+
declare function f1<const T>(x: T): T;
3+
4+
const x11 = f1('a');
5+
const x12 = f1(['a', ['b', 'c']]);
6+
const x13 = f1({ a: 1, b: "c", d: ["e", 2, true, { f: "g" }] });
7+
8+
declare function f2<const T, U>(x: T | undefined): T;
9+
10+
const x21 = f2('a');
11+
const x22 = f2(['a', ['b', 'c']]);
12+
const x23 = f2({ a: 1, b: "c", d: ["e", 2, true, { f: "g" }] });
13+
14+
declare function f3<const T>(x: T): T[];
15+
16+
const x31 = f3("hello");
17+
const x32 = f3("hello");
18+
19+
declare function f4<const T>(obj: [T, T]): T;
20+
21+
const x41 = f4([[1, 'x'], [2, 'y']]);
22+
const x42 = f4([{ a: 1, b: 'x' }, { a: 2, b: 'y' }]);
23+
24+
declare function f5<const T>(obj: { x: T, y: T }): T;
25+
26+
const x51 = f5({ x: [1, 'x'], y: [2, 'y'] });
27+
const x52 = f5({ x: { a: 1, b: 'x' }, y: { a: 2, b: 'y' } });
28+
29+
declare function f6<const T extends readonly unknown[]>(...args: T): T;
30+
31+
const x61 = f6(1, 'b', { a: 1, b: 'x' });
32+
33+
class C1<const T> {
34+
constructor(x: T) {}
35+
foo<const U>(x: U) { return x; }
36+
}
37+
38+
const c71 = new C1({ a: 1, b: "c", d: ["e", 2, true, { f: "g" }] });
39+
const c72 = c71.foo(['a', ['b', 'c']]);
40+
41+
interface I1<const T> { x: T } // Error
42+
43+
type T1<const T> = T; // Error
44+
45+
// Corrected repro from #51745
46+
47+
type Obj = { a: { b: { c: "123" } } };
48+
49+
type GetPath<T, P> =
50+
P extends readonly [] ? T :
51+
P extends readonly [infer A extends keyof T, ...infer Rest] ? GetPath<T[A], Rest> :
52+
never;
53+
54+
function set<T, const P extends readonly string[]>(obj: T, path: P, value: GetPath<T, P>) {}
55+
56+
declare let obj: Obj;
57+
declare let value: "123";
58+
59+
set(obj, ['a', 'b', 'c'], value);
60+
61+
62+
//// [typeParameterConstModifiers.js]
63+
"use strict";
64+
var x11 = f1('a');
65+
var x12 = f1(['a', ['b', 'c']]);
66+
var x13 = f1({ a: 1, b: "c", d: ["e", 2, true, { f: "g" }] });
67+
var x21 = f2('a');
68+
var x22 = f2(['a', ['b', 'c']]);
69+
var x23 = f2({ a: 1, b: "c", d: ["e", 2, true, { f: "g" }] });
70+
var x31 = f3("hello");
71+
var x32 = f3("hello");
72+
var x41 = f4([[1, 'x'], [2, 'y']]);
73+
var x42 = f4([{ a: 1, b: 'x' }, { a: 2, b: 'y' }]);
74+
var x51 = f5({ x: [1, 'x'], y: [2, 'y'] });
75+
var x52 = f5({ x: { a: 1, b: 'x' }, y: { a: 2, b: 'y' } });
76+
var x61 = f6(1, 'b', { a: 1, b: 'x' });
77+
var C1 = /** @class */ (function () {
78+
function C1(x) {
79+
}
80+
C1.prototype.foo = function (x) { return x; };
81+
return C1;
82+
}());
83+
var c71 = new C1({ a: 1, b: "c", d: ["e", 2, true, { f: "g" }] });
84+
var c72 = c71.foo(['a', ['b', 'c']]);
85+
function set(obj, path, value) { }
86+
set(obj, ['a', 'b', 'c'], value);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
=== tests/cases/conformance/types/typeParameters/typeParameterLists/typeParameterConstModifiers.ts ===
2+
declare function f1<const T>(x: T): T;
3+
>f1 : Symbol(f1, Decl(typeParameterConstModifiers.ts, 0, 0))
4+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 0, 20))
5+
>x : Symbol(x, Decl(typeParameterConstModifiers.ts, 0, 29))
6+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 0, 20))
7+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 0, 20))
8+
9+
const x11 = f1('a');
10+
>x11 : Symbol(x11, Decl(typeParameterConstModifiers.ts, 2, 5))
11+
>f1 : Symbol(f1, Decl(typeParameterConstModifiers.ts, 0, 0))
12+
13+
const x12 = f1(['a', ['b', 'c']]);
14+
>x12 : Symbol(x12, Decl(typeParameterConstModifiers.ts, 3, 5))
15+
>f1 : Symbol(f1, Decl(typeParameterConstModifiers.ts, 0, 0))
16+
17+
const x13 = f1({ a: 1, b: "c", d: ["e", 2, true, { f: "g" }] });
18+
>x13 : Symbol(x13, Decl(typeParameterConstModifiers.ts, 4, 5))
19+
>f1 : Symbol(f1, Decl(typeParameterConstModifiers.ts, 0, 0))
20+
>a : Symbol(a, Decl(typeParameterConstModifiers.ts, 4, 16))
21+
>b : Symbol(b, Decl(typeParameterConstModifiers.ts, 4, 22))
22+
>d : Symbol(d, Decl(typeParameterConstModifiers.ts, 4, 30))
23+
>f : Symbol(f, Decl(typeParameterConstModifiers.ts, 4, 50))
24+
25+
declare function f2<const T, U>(x: T | undefined): T;
26+
>f2 : Symbol(f2, Decl(typeParameterConstModifiers.ts, 4, 64))
27+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 6, 20))
28+
>U : Symbol(U, Decl(typeParameterConstModifiers.ts, 6, 28))
29+
>x : Symbol(x, Decl(typeParameterConstModifiers.ts, 6, 32))
30+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 6, 20))
31+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 6, 20))
32+
33+
const x21 = f2('a');
34+
>x21 : Symbol(x21, Decl(typeParameterConstModifiers.ts, 8, 5))
35+
>f2 : Symbol(f2, Decl(typeParameterConstModifiers.ts, 4, 64))
36+
37+
const x22 = f2(['a', ['b', 'c']]);
38+
>x22 : Symbol(x22, Decl(typeParameterConstModifiers.ts, 9, 5))
39+
>f2 : Symbol(f2, Decl(typeParameterConstModifiers.ts, 4, 64))
40+
41+
const x23 = f2({ a: 1, b: "c", d: ["e", 2, true, { f: "g" }] });
42+
>x23 : Symbol(x23, Decl(typeParameterConstModifiers.ts, 10, 5))
43+
>f2 : Symbol(f2, Decl(typeParameterConstModifiers.ts, 4, 64))
44+
>a : Symbol(a, Decl(typeParameterConstModifiers.ts, 10, 16))
45+
>b : Symbol(b, Decl(typeParameterConstModifiers.ts, 10, 22))
46+
>d : Symbol(d, Decl(typeParameterConstModifiers.ts, 10, 30))
47+
>f : Symbol(f, Decl(typeParameterConstModifiers.ts, 10, 50))
48+
49+
declare function f3<const T>(x: T): T[];
50+
>f3 : Symbol(f3, Decl(typeParameterConstModifiers.ts, 10, 64))
51+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 12, 20))
52+
>x : Symbol(x, Decl(typeParameterConstModifiers.ts, 12, 29))
53+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 12, 20))
54+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 12, 20))
55+
56+
const x31 = f3("hello");
57+
>x31 : Symbol(x31, Decl(typeParameterConstModifiers.ts, 14, 5))
58+
>f3 : Symbol(f3, Decl(typeParameterConstModifiers.ts, 10, 64))
59+
60+
const x32 = f3("hello");
61+
>x32 : Symbol(x32, Decl(typeParameterConstModifiers.ts, 15, 5))
62+
>f3 : Symbol(f3, Decl(typeParameterConstModifiers.ts, 10, 64))
63+
64+
declare function f4<const T>(obj: [T, T]): T;
65+
>f4 : Symbol(f4, Decl(typeParameterConstModifiers.ts, 15, 24))
66+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 17, 20))
67+
>obj : Symbol(obj, Decl(typeParameterConstModifiers.ts, 17, 29))
68+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 17, 20))
69+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 17, 20))
70+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 17, 20))
71+
72+
const x41 = f4([[1, 'x'], [2, 'y']]);
73+
>x41 : Symbol(x41, Decl(typeParameterConstModifiers.ts, 19, 5))
74+
>f4 : Symbol(f4, Decl(typeParameterConstModifiers.ts, 15, 24))
75+
76+
const x42 = f4([{ a: 1, b: 'x' }, { a: 2, b: 'y' }]);
77+
>x42 : Symbol(x42, Decl(typeParameterConstModifiers.ts, 20, 5))
78+
>f4 : Symbol(f4, Decl(typeParameterConstModifiers.ts, 15, 24))
79+
>a : Symbol(a, Decl(typeParameterConstModifiers.ts, 20, 17))
80+
>b : Symbol(b, Decl(typeParameterConstModifiers.ts, 20, 23))
81+
>a : Symbol(a, Decl(typeParameterConstModifiers.ts, 20, 35))
82+
>b : Symbol(b, Decl(typeParameterConstModifiers.ts, 20, 41))
83+
84+
declare function f5<const T>(obj: { x: T, y: T }): T;
85+
>f5 : Symbol(f5, Decl(typeParameterConstModifiers.ts, 20, 53))
86+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 22, 20))
87+
>obj : Symbol(obj, Decl(typeParameterConstModifiers.ts, 22, 29))
88+
>x : Symbol(x, Decl(typeParameterConstModifiers.ts, 22, 35))
89+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 22, 20))
90+
>y : Symbol(y, Decl(typeParameterConstModifiers.ts, 22, 41))
91+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 22, 20))
92+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 22, 20))
93+
94+
const x51 = f5({ x: [1, 'x'], y: [2, 'y'] });
95+
>x51 : Symbol(x51, Decl(typeParameterConstModifiers.ts, 24, 5))
96+
>f5 : Symbol(f5, Decl(typeParameterConstModifiers.ts, 20, 53))
97+
>x : Symbol(x, Decl(typeParameterConstModifiers.ts, 24, 16))
98+
>y : Symbol(y, Decl(typeParameterConstModifiers.ts, 24, 29))
99+
100+
const x52 = f5({ x: { a: 1, b: 'x' }, y: { a: 2, b: 'y' } });
101+
>x52 : Symbol(x52, Decl(typeParameterConstModifiers.ts, 25, 5))
102+
>f5 : Symbol(f5, Decl(typeParameterConstModifiers.ts, 20, 53))
103+
>x : Symbol(x, Decl(typeParameterConstModifiers.ts, 25, 16))
104+
>a : Symbol(a, Decl(typeParameterConstModifiers.ts, 25, 21))
105+
>b : Symbol(b, Decl(typeParameterConstModifiers.ts, 25, 27))
106+
>y : Symbol(y, Decl(typeParameterConstModifiers.ts, 25, 37))
107+
>a : Symbol(a, Decl(typeParameterConstModifiers.ts, 25, 42))
108+
>b : Symbol(b, Decl(typeParameterConstModifiers.ts, 25, 48))
109+
110+
declare function f6<const T extends readonly unknown[]>(...args: T): T;
111+
>f6 : Symbol(f6, Decl(typeParameterConstModifiers.ts, 25, 61))
112+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 27, 20))
113+
>args : Symbol(args, Decl(typeParameterConstModifiers.ts, 27, 56))
114+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 27, 20))
115+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 27, 20))
116+
117+
const x61 = f6(1, 'b', { a: 1, b: 'x' });
118+
>x61 : Symbol(x61, Decl(typeParameterConstModifiers.ts, 29, 5))
119+
>f6 : Symbol(f6, Decl(typeParameterConstModifiers.ts, 25, 61))
120+
>a : Symbol(a, Decl(typeParameterConstModifiers.ts, 29, 24))
121+
>b : Symbol(b, Decl(typeParameterConstModifiers.ts, 29, 30))
122+
123+
class C1<const T> {
124+
>C1 : Symbol(C1, Decl(typeParameterConstModifiers.ts, 29, 41))
125+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 31, 9))
126+
127+
constructor(x: T) {}
128+
>x : Symbol(x, Decl(typeParameterConstModifiers.ts, 32, 16))
129+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 31, 9))
130+
131+
foo<const U>(x: U) { return x; }
132+
>foo : Symbol(C1.foo, Decl(typeParameterConstModifiers.ts, 32, 24))
133+
>U : Symbol(U, Decl(typeParameterConstModifiers.ts, 33, 8))
134+
>x : Symbol(x, Decl(typeParameterConstModifiers.ts, 33, 17))
135+
>U : Symbol(U, Decl(typeParameterConstModifiers.ts, 33, 8))
136+
>x : Symbol(x, Decl(typeParameterConstModifiers.ts, 33, 17))
137+
}
138+
139+
const c71 = new C1({ a: 1, b: "c", d: ["e", 2, true, { f: "g" }] });
140+
>c71 : Symbol(c71, Decl(typeParameterConstModifiers.ts, 36, 5))
141+
>C1 : Symbol(C1, Decl(typeParameterConstModifiers.ts, 29, 41))
142+
>a : Symbol(a, Decl(typeParameterConstModifiers.ts, 36, 20))
143+
>b : Symbol(b, Decl(typeParameterConstModifiers.ts, 36, 26))
144+
>d : Symbol(d, Decl(typeParameterConstModifiers.ts, 36, 34))
145+
>f : Symbol(f, Decl(typeParameterConstModifiers.ts, 36, 54))
146+
147+
const c72 = c71.foo(['a', ['b', 'c']]);
148+
>c72 : Symbol(c72, Decl(typeParameterConstModifiers.ts, 37, 5))
149+
>c71.foo : Symbol(C1.foo, Decl(typeParameterConstModifiers.ts, 32, 24))
150+
>c71 : Symbol(c71, Decl(typeParameterConstModifiers.ts, 36, 5))
151+
>foo : Symbol(C1.foo, Decl(typeParameterConstModifiers.ts, 32, 24))
152+
153+
interface I1<const T> { x: T } // Error
154+
>I1 : Symbol(I1, Decl(typeParameterConstModifiers.ts, 37, 39))
155+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 39, 13))
156+
>x : Symbol(I1.x, Decl(typeParameterConstModifiers.ts, 39, 23))
157+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 39, 13))
158+
159+
type T1<const T> = T; // Error
160+
>T1 : Symbol(T1, Decl(typeParameterConstModifiers.ts, 39, 30))
161+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 41, 8))
162+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 41, 8))
163+
164+
// Corrected repro from #51745
165+
166+
type Obj = { a: { b: { c: "123" } } };
167+
>Obj : Symbol(Obj, Decl(typeParameterConstModifiers.ts, 41, 21))
168+
>a : Symbol(a, Decl(typeParameterConstModifiers.ts, 45, 12))
169+
>b : Symbol(b, Decl(typeParameterConstModifiers.ts, 45, 17))
170+
>c : Symbol(c, Decl(typeParameterConstModifiers.ts, 45, 22))
171+
172+
type GetPath<T, P> =
173+
>GetPath : Symbol(GetPath, Decl(typeParameterConstModifiers.ts, 45, 38))
174+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 47, 13))
175+
>P : Symbol(P, Decl(typeParameterConstModifiers.ts, 47, 15))
176+
177+
P extends readonly [] ? T :
178+
>P : Symbol(P, Decl(typeParameterConstModifiers.ts, 47, 15))
179+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 47, 13))
180+
181+
P extends readonly [infer A extends keyof T, ...infer Rest] ? GetPath<T[A], Rest> :
182+
>P : Symbol(P, Decl(typeParameterConstModifiers.ts, 47, 15))
183+
>A : Symbol(A, Decl(typeParameterConstModifiers.ts, 49, 29))
184+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 47, 13))
185+
>Rest : Symbol(Rest, Decl(typeParameterConstModifiers.ts, 49, 57))
186+
>GetPath : Symbol(GetPath, Decl(typeParameterConstModifiers.ts, 45, 38))
187+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 47, 13))
188+
>A : Symbol(A, Decl(typeParameterConstModifiers.ts, 49, 29))
189+
>Rest : Symbol(Rest, Decl(typeParameterConstModifiers.ts, 49, 57))
190+
191+
never;
192+
193+
function set<T, const P extends readonly string[]>(obj: T, path: P, value: GetPath<T, P>) {}
194+
>set : Symbol(set, Decl(typeParameterConstModifiers.ts, 50, 10))
195+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 52, 13))
196+
>P : Symbol(P, Decl(typeParameterConstModifiers.ts, 52, 15))
197+
>obj : Symbol(obj, Decl(typeParameterConstModifiers.ts, 52, 51))
198+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 52, 13))
199+
>path : Symbol(path, Decl(typeParameterConstModifiers.ts, 52, 58))
200+
>P : Symbol(P, Decl(typeParameterConstModifiers.ts, 52, 15))
201+
>value : Symbol(value, Decl(typeParameterConstModifiers.ts, 52, 67))
202+
>GetPath : Symbol(GetPath, Decl(typeParameterConstModifiers.ts, 45, 38))
203+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 52, 13))
204+
>P : Symbol(P, Decl(typeParameterConstModifiers.ts, 52, 15))
205+
206+
declare let obj: Obj;
207+
>obj : Symbol(obj, Decl(typeParameterConstModifiers.ts, 54, 11))
208+
>Obj : Symbol(Obj, Decl(typeParameterConstModifiers.ts, 41, 21))
209+
210+
declare let value: "123";
211+
>value : Symbol(value, Decl(typeParameterConstModifiers.ts, 55, 11))
212+
213+
set(obj, ['a', 'b', 'c'], value);
214+
>set : Symbol(set, Decl(typeParameterConstModifiers.ts, 50, 10))
215+
>obj : Symbol(obj, Decl(typeParameterConstModifiers.ts, 54, 11))
216+
>value : Symbol(value, Decl(typeParameterConstModifiers.ts, 55, 11))
217+

0 commit comments

Comments
 (0)