Skip to content

Commit f6833cd

Browse files
committed
Add more tests
1 parent 8f07af9 commit f6833cd

File tree

5 files changed

+484
-3
lines changed

5 files changed

+484
-3
lines changed

tests/baselines/reference/typeParameterConstModifiers.errors.txt

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,53 @@ typeParameterConstModifiers.ts(55,9): error TS1277: 'const' modifier can only ap
130130
const t2_55033 = factory_55033((a: { test: number }, b: string) => {})(
131131
{ test: 123 } as const,
132132
"some string"
133-
);
133+
);
134+
135+
// Same with non-readonly constraint
136+
137+
function factory_55033_2<const T extends unknown[]>(cb: (...args: T) => void) {
138+
return function call<const K extends T>(...args: K): K {
139+
return {} as K;
140+
};
141+
}
142+
143+
const t1_55033_2 = factory_55033_2((a: { test: number }, b: string) => {})(
144+
{ test: 123 },
145+
"some string"
146+
);
147+
148+
const t2_55033_2 = factory_55033_2((a: { test: number }, b: string) => {})(
149+
{ test: 123 } as const,
150+
"some string"
151+
);
152+
153+
// Repro from https://github.com/microsoft/TypeScript/issues/51931
154+
155+
declare function fn<const T extends any[]>(...args: T): T;
156+
157+
const a = fn("a", false);
158+
159+
// More examples of non-readonly constraints
160+
161+
declare function fa1<const T extends unknown[]>(args: T): T;
162+
declare function fa2<const T extends readonly unknown[]>(args: T): T;
163+
164+
fa1(["hello", 42]);
165+
fa2(["hello", 42]);
166+
167+
declare function fb1<const T extends unknown[]>(...args: T): T;
168+
declare function fb2<const T extends readonly unknown[]>(...args: T): T;
169+
170+
fb1("hello", 42);
171+
fb2("hello", 42);
172+
173+
declare function fc1<const T extends unknown[]>(f: (...args: T) => void, ...args: T): T;
174+
declare function fc2<const T extends readonly unknown[]>(f: (...args: T) => void, ...args: T): T;
175+
176+
fc1((a: string, b: number) => {}, "hello", 42);
177+
fc2((a: string, b: number) => {}, "hello", 42);
178+
179+
declare function fn1<const T extends { foo: unknown[] }[]>(...args: T): T;
180+
181+
fn1({ foo: ["hello", 123] }, { foo: [true]});
182+

tests/baselines/reference/typeParameterConstModifiers.js

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,56 @@ const t1_55033 = factory_55033((a: { test: number }, b: string) => {})(
124124
const t2_55033 = factory_55033((a: { test: number }, b: string) => {})(
125125
{ test: 123 } as const,
126126
"some string"
127-
);
127+
);
128+
129+
// Same with non-readonly constraint
130+
131+
function factory_55033_2<const T extends unknown[]>(cb: (...args: T) => void) {
132+
return function call<const K extends T>(...args: K): K {
133+
return {} as K;
134+
};
135+
}
136+
137+
const t1_55033_2 = factory_55033_2((a: { test: number }, b: string) => {})(
138+
{ test: 123 },
139+
"some string"
140+
);
141+
142+
const t2_55033_2 = factory_55033_2((a: { test: number }, b: string) => {})(
143+
{ test: 123 } as const,
144+
"some string"
145+
);
146+
147+
// Repro from https://github.com/microsoft/TypeScript/issues/51931
148+
149+
declare function fn<const T extends any[]>(...args: T): T;
150+
151+
const a = fn("a", false);
152+
153+
// More examples of non-readonly constraints
154+
155+
declare function fa1<const T extends unknown[]>(args: T): T;
156+
declare function fa2<const T extends readonly unknown[]>(args: T): T;
157+
158+
fa1(["hello", 42]);
159+
fa2(["hello", 42]);
160+
161+
declare function fb1<const T extends unknown[]>(...args: T): T;
162+
declare function fb2<const T extends readonly unknown[]>(...args: T): T;
163+
164+
fb1("hello", 42);
165+
fb2("hello", 42);
166+
167+
declare function fc1<const T extends unknown[]>(f: (...args: T) => void, ...args: T): T;
168+
declare function fc2<const T extends readonly unknown[]>(f: (...args: T) => void, ...args: T): T;
169+
170+
fc1((a: string, b: number) => {}, "hello", 42);
171+
fc2((a: string, b: number) => {}, "hello", 42);
172+
173+
declare function fn1<const T extends { foo: unknown[] }[]>(...args: T): T;
174+
175+
fn1({ foo: ["hello", 123] }, { foo: [true]});
176+
128177

129178
//// [typeParameterConstModifiers.js]
130179
"use strict";
@@ -193,3 +242,23 @@ function factory_55033(cb) {
193242
}
194243
var t1_55033 = factory_55033(function (a, b) { })({ test: 123 }, "some string");
195244
var t2_55033 = factory_55033(function (a, b) { })({ test: 123 }, "some string");
245+
// Same with non-readonly constraint
246+
function factory_55033_2(cb) {
247+
return function call() {
248+
var args = [];
249+
for (var _i = 0; _i < arguments.length; _i++) {
250+
args[_i] = arguments[_i];
251+
}
252+
return {};
253+
};
254+
}
255+
var t1_55033_2 = factory_55033_2(function (a, b) { })({ test: 123 }, "some string");
256+
var t2_55033_2 = factory_55033_2(function (a, b) { })({ test: 123 }, "some string");
257+
var a = fn("a", false);
258+
fa1(["hello", 42]);
259+
fa2(["hello", 42]);
260+
fb1("hello", 42);
261+
fb2("hello", 42);
262+
fc1(function (a, b) { }, "hello", 42);
263+
fc2(function (a, b) { }, "hello", 42);
264+
fn1({ foo: ["hello", 123] }, { foo: [true] });

tests/baselines/reference/typeParameterConstModifiers.symbols

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,3 +426,152 @@ const t2_55033 = factory_55033((a: { test: number }, b: string) => {})(
426426

427427
"some string"
428428
);
429+
430+
// Same with non-readonly constraint
431+
432+
function factory_55033_2<const T extends unknown[]>(cb: (...args: T) => void) {
433+
>factory_55033_2 : Symbol(factory_55033_2, Decl(typeParameterConstModifiers.ts, 123, 2))
434+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 127, 25))
435+
>cb : Symbol(cb, Decl(typeParameterConstModifiers.ts, 127, 52))
436+
>args : Symbol(args, Decl(typeParameterConstModifiers.ts, 127, 57))
437+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 127, 25))
438+
439+
return function call<const K extends T>(...args: K): K {
440+
>call : Symbol(call, Decl(typeParameterConstModifiers.ts, 128, 10))
441+
>K : Symbol(K, Decl(typeParameterConstModifiers.ts, 128, 25))
442+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 127, 25))
443+
>args : Symbol(args, Decl(typeParameterConstModifiers.ts, 128, 44))
444+
>K : Symbol(K, Decl(typeParameterConstModifiers.ts, 128, 25))
445+
>K : Symbol(K, Decl(typeParameterConstModifiers.ts, 128, 25))
446+
447+
return {} as K;
448+
>K : Symbol(K, Decl(typeParameterConstModifiers.ts, 128, 25))
449+
450+
};
451+
}
452+
453+
const t1_55033_2 = factory_55033_2((a: { test: number }, b: string) => {})(
454+
>t1_55033_2 : Symbol(t1_55033_2, Decl(typeParameterConstModifiers.ts, 133, 5))
455+
>factory_55033_2 : Symbol(factory_55033_2, Decl(typeParameterConstModifiers.ts, 123, 2))
456+
>a : Symbol(a, Decl(typeParameterConstModifiers.ts, 133, 36))
457+
>test : Symbol(test, Decl(typeParameterConstModifiers.ts, 133, 40))
458+
>b : Symbol(b, Decl(typeParameterConstModifiers.ts, 133, 56))
459+
460+
{ test: 123 },
461+
>test : Symbol(test, Decl(typeParameterConstModifiers.ts, 134, 5))
462+
463+
"some string"
464+
);
465+
466+
const t2_55033_2 = factory_55033_2((a: { test: number }, b: string) => {})(
467+
>t2_55033_2 : Symbol(t2_55033_2, Decl(typeParameterConstModifiers.ts, 138, 5))
468+
>factory_55033_2 : Symbol(factory_55033_2, Decl(typeParameterConstModifiers.ts, 123, 2))
469+
>a : Symbol(a, Decl(typeParameterConstModifiers.ts, 138, 36))
470+
>test : Symbol(test, Decl(typeParameterConstModifiers.ts, 138, 40))
471+
>b : Symbol(b, Decl(typeParameterConstModifiers.ts, 138, 56))
472+
473+
{ test: 123 } as const,
474+
>test : Symbol(test, Decl(typeParameterConstModifiers.ts, 139, 5))
475+
>const : Symbol(const)
476+
477+
"some string"
478+
);
479+
480+
// Repro from https://github.com/microsoft/TypeScript/issues/51931
481+
482+
declare function fn<const T extends any[]>(...args: T): T;
483+
>fn : Symbol(fn, Decl(typeParameterConstModifiers.ts, 141, 2))
484+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 145, 20))
485+
>args : Symbol(args, Decl(typeParameterConstModifiers.ts, 145, 43))
486+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 145, 20))
487+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 145, 20))
488+
489+
const a = fn("a", false);
490+
>a : Symbol(a, Decl(typeParameterConstModifiers.ts, 147, 5))
491+
>fn : Symbol(fn, Decl(typeParameterConstModifiers.ts, 141, 2))
492+
493+
// More examples of non-readonly constraints
494+
495+
declare function fa1<const T extends unknown[]>(args: T): T;
496+
>fa1 : Symbol(fa1, Decl(typeParameterConstModifiers.ts, 147, 25))
497+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 151, 21))
498+
>args : Symbol(args, Decl(typeParameterConstModifiers.ts, 151, 48))
499+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 151, 21))
500+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 151, 21))
501+
502+
declare function fa2<const T extends readonly unknown[]>(args: T): T;
503+
>fa2 : Symbol(fa2, Decl(typeParameterConstModifiers.ts, 151, 60))
504+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 152, 21))
505+
>args : Symbol(args, Decl(typeParameterConstModifiers.ts, 152, 57))
506+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 152, 21))
507+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 152, 21))
508+
509+
fa1(["hello", 42]);
510+
>fa1 : Symbol(fa1, Decl(typeParameterConstModifiers.ts, 147, 25))
511+
512+
fa2(["hello", 42]);
513+
>fa2 : Symbol(fa2, Decl(typeParameterConstModifiers.ts, 151, 60))
514+
515+
declare function fb1<const T extends unknown[]>(...args: T): T;
516+
>fb1 : Symbol(fb1, Decl(typeParameterConstModifiers.ts, 155, 19))
517+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 157, 21))
518+
>args : Symbol(args, Decl(typeParameterConstModifiers.ts, 157, 48))
519+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 157, 21))
520+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 157, 21))
521+
522+
declare function fb2<const T extends readonly unknown[]>(...args: T): T;
523+
>fb2 : Symbol(fb2, Decl(typeParameterConstModifiers.ts, 157, 63))
524+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 158, 21))
525+
>args : Symbol(args, Decl(typeParameterConstModifiers.ts, 158, 57))
526+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 158, 21))
527+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 158, 21))
528+
529+
fb1("hello", 42);
530+
>fb1 : Symbol(fb1, Decl(typeParameterConstModifiers.ts, 155, 19))
531+
532+
fb2("hello", 42);
533+
>fb2 : Symbol(fb2, Decl(typeParameterConstModifiers.ts, 157, 63))
534+
535+
declare function fc1<const T extends unknown[]>(f: (...args: T) => void, ...args: T): T;
536+
>fc1 : Symbol(fc1, Decl(typeParameterConstModifiers.ts, 161, 17))
537+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 163, 21))
538+
>f : Symbol(f, Decl(typeParameterConstModifiers.ts, 163, 48))
539+
>args : Symbol(args, Decl(typeParameterConstModifiers.ts, 163, 52))
540+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 163, 21))
541+
>args : Symbol(args, Decl(typeParameterConstModifiers.ts, 163, 72))
542+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 163, 21))
543+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 163, 21))
544+
545+
declare function fc2<const T extends readonly unknown[]>(f: (...args: T) => void, ...args: T): T;
546+
>fc2 : Symbol(fc2, Decl(typeParameterConstModifiers.ts, 163, 88))
547+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 164, 21))
548+
>f : Symbol(f, Decl(typeParameterConstModifiers.ts, 164, 57))
549+
>args : Symbol(args, Decl(typeParameterConstModifiers.ts, 164, 61))
550+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 164, 21))
551+
>args : Symbol(args, Decl(typeParameterConstModifiers.ts, 164, 81))
552+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 164, 21))
553+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 164, 21))
554+
555+
fc1((a: string, b: number) => {}, "hello", 42);
556+
>fc1 : Symbol(fc1, Decl(typeParameterConstModifiers.ts, 161, 17))
557+
>a : Symbol(a, Decl(typeParameterConstModifiers.ts, 166, 5))
558+
>b : Symbol(b, Decl(typeParameterConstModifiers.ts, 166, 15))
559+
560+
fc2((a: string, b: number) => {}, "hello", 42);
561+
>fc2 : Symbol(fc2, Decl(typeParameterConstModifiers.ts, 163, 88))
562+
>a : Symbol(a, Decl(typeParameterConstModifiers.ts, 167, 5))
563+
>b : Symbol(b, Decl(typeParameterConstModifiers.ts, 167, 15))
564+
565+
declare function fn1<const T extends { foo: unknown[] }[]>(...args: T): T;
566+
>fn1 : Symbol(fn1, Decl(typeParameterConstModifiers.ts, 167, 47))
567+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 169, 21))
568+
>foo : Symbol(foo, Decl(typeParameterConstModifiers.ts, 169, 38))
569+
>args : Symbol(args, Decl(typeParameterConstModifiers.ts, 169, 59))
570+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 169, 21))
571+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 169, 21))
572+
573+
fn1({ foo: ["hello", 123] }, { foo: [true]});
574+
>fn1 : Symbol(fn1, Decl(typeParameterConstModifiers.ts, 167, 47))
575+
>foo : Symbol(foo, Decl(typeParameterConstModifiers.ts, 171, 5))
576+
>foo : Symbol(foo, Decl(typeParameterConstModifiers.ts, 171, 30))
577+

0 commit comments

Comments
 (0)