Skip to content

Commit 36e9570

Browse files
authored
Propagate constness of type parameters in variadic tuples (#52129)
1 parent ef58695 commit 36e9570

File tree

6 files changed

+88
-0
lines changed

6 files changed

+88
-0
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13256,6 +13256,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
1325613256

1325713257
function isConstTypeVariable(type: Type): boolean {
1325813258
return !!(type.flags & TypeFlags.TypeParameter && some((type as TypeParameter).symbol?.declarations, d => hasSyntacticModifier(d, ModifierFlags.Const)) ||
13259+
isGenericTupleType(type) && findIndex(getTypeArguments(type), (t, i) => !!(type.target.elementFlags[i] & ElementFlags.Variadic) && isConstTypeVariable(t)) >= 0 ||
1325913260
type.flags & TypeFlags.IndexedAccess && isConstTypeVariable((type as IndexedAccessType).objectType));
1326013261
}
1326113262

tests/baselines/reference/typeParameterConstModifiers.errors.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,14 @@ tests/cases/conformance/types/typeParameters/typeParameterLists/typeParameterCon
7979
declare let value: "123";
8080

8181
set(obj, ['a', 'b', 'c'], value);
82+
83+
// Repro from #52007
84+
85+
declare function inners<const T extends readonly any[]>(...args: readonly [unknown, ...T, unknown]): T;
86+
87+
const test = inners(1,2,3,4,5);
88+
89+
declare function inners2<const T extends readonly any[]>(args: readonly [unknown, ...T, unknown]): T;
90+
91+
const test2 = inners2([1,2,3,4,5]);
8292

tests/baselines/reference/typeParameterConstModifiers.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@ declare let obj: Obj;
7171
declare let value: "123";
7272

7373
set(obj, ['a', 'b', 'c'], value);
74+
75+
// Repro from #52007
76+
77+
declare function inners<const T extends readonly any[]>(...args: readonly [unknown, ...T, unknown]): T;
78+
79+
const test = inners(1,2,3,4,5);
80+
81+
declare function inners2<const T extends readonly any[]>(args: readonly [unknown, ...T, unknown]): T;
82+
83+
const test2 = inners2([1,2,3,4,5]);
7484

7585

7686
//// [typeParameterConstModifiers.js]
@@ -105,3 +115,5 @@ var fx1 = function (x) { return x; };
105115
var fx2 = function (x) { return x; };
106116
function set(obj, path, value) { }
107117
set(obj, ['a', 'b', 'c'], value);
118+
var test = inners(1, 2, 3, 4, 5);
119+
var test2 = inners2([1, 2, 3, 4, 5]);

tests/baselines/reference/typeParameterConstModifiers.symbols

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,3 +272,27 @@ set(obj, ['a', 'b', 'c'], value);
272272
>obj : Symbol(obj, Decl(typeParameterConstModifiers.ts, 68, 11))
273273
>value : Symbol(value, Decl(typeParameterConstModifiers.ts, 69, 11))
274274

275+
// Repro from #52007
276+
277+
declare function inners<const T extends readonly any[]>(...args: readonly [unknown, ...T, unknown]): T;
278+
>inners : Symbol(inners, Decl(typeParameterConstModifiers.ts, 71, 33))
279+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 75, 24))
280+
>args : Symbol(args, Decl(typeParameterConstModifiers.ts, 75, 56))
281+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 75, 24))
282+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 75, 24))
283+
284+
const test = inners(1,2,3,4,5);
285+
>test : Symbol(test, Decl(typeParameterConstModifiers.ts, 77, 5))
286+
>inners : Symbol(inners, Decl(typeParameterConstModifiers.ts, 71, 33))
287+
288+
declare function inners2<const T extends readonly any[]>(args: readonly [unknown, ...T, unknown]): T;
289+
>inners2 : Symbol(inners2, Decl(typeParameterConstModifiers.ts, 77, 31))
290+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 79, 25))
291+
>args : Symbol(args, Decl(typeParameterConstModifiers.ts, 79, 57))
292+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 79, 25))
293+
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 79, 25))
294+
295+
const test2 = inners2([1,2,3,4,5]);
296+
>test2 : Symbol(test2, Decl(typeParameterConstModifiers.ts, 81, 5))
297+
>inners2 : Symbol(inners2, Decl(typeParameterConstModifiers.ts, 77, 31))
298+

tests/baselines/reference/typeParameterConstModifiers.types

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,3 +300,34 @@ set(obj, ['a', 'b', 'c'], value);
300300
>'c' : "c"
301301
>value : "123"
302302

303+
// Repro from #52007
304+
305+
declare function inners<const T extends readonly any[]>(...args: readonly [unknown, ...T, unknown]): T;
306+
>inners : <const T extends readonly any[]>(...args: readonly [unknown, ...T, unknown]) => T
307+
>args : readonly [unknown, ...T, unknown]
308+
309+
const test = inners(1,2,3,4,5);
310+
>test : [2, 3, 4]
311+
>inners(1,2,3,4,5) : [2, 3, 4]
312+
>inners : <const T extends readonly any[]>(...args: readonly [unknown, ...T, unknown]) => T
313+
>1 : 1
314+
>2 : 2
315+
>3 : 3
316+
>4 : 4
317+
>5 : 5
318+
319+
declare function inners2<const T extends readonly any[]>(args: readonly [unknown, ...T, unknown]): T;
320+
>inners2 : <const T extends readonly any[]>(args: readonly [unknown, ...T, unknown]) => T
321+
>args : readonly [unknown, ...T, unknown]
322+
323+
const test2 = inners2([1,2,3,4,5]);
324+
>test2 : [2, 3, 4]
325+
>inners2([1,2,3,4,5]) : [2, 3, 4]
326+
>inners2 : <const T extends readonly any[]>(args: readonly [unknown, ...T, unknown]) => T
327+
>[1,2,3,4,5] : [number, 2, 3, 4, number]
328+
>1 : 1
329+
>2 : 2
330+
>3 : 3
331+
>4 : 4
332+
>5 : 5
333+

tests/cases/conformance/types/typeParameters/typeParameterLists/typeParameterConstModifiers.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,13 @@ declare let obj: Obj;
7272
declare let value: "123";
7373

7474
set(obj, ['a', 'b', 'c'], value);
75+
76+
// Repro from #52007
77+
78+
declare function inners<const T extends readonly any[]>(...args: readonly [unknown, ...T, unknown]): T;
79+
80+
const test = inners(1,2,3,4,5);
81+
82+
declare function inners2<const T extends readonly any[]>(args: readonly [unknown, ...T, unknown]): T;
83+
84+
const test2 = inners2([1,2,3,4,5]);

0 commit comments

Comments
 (0)