Propagate constness of type parameters in variadic tuples (#52129)

This commit is contained in:
Anders Hejlsberg
2023-01-13 07:23:04 -08:00
committed by GitHub
parent ef58695fbc
commit 36e9570933
6 changed files with 88 additions and 0 deletions
+1
View File
@@ -13256,6 +13256,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
function isConstTypeVariable(type: Type): boolean {
return !!(type.flags & TypeFlags.TypeParameter && some((type as TypeParameter).symbol?.declarations, d => hasSyntacticModifier(d, ModifierFlags.Const)) ||
isGenericTupleType(type) && findIndex(getTypeArguments(type), (t, i) => !!(type.target.elementFlags[i] & ElementFlags.Variadic) && isConstTypeVariable(t)) >= 0 ||
type.flags & TypeFlags.IndexedAccess && isConstTypeVariable((type as IndexedAccessType).objectType));
}
@@ -79,4 +79,14 @@ tests/cases/conformance/types/typeParameters/typeParameterLists/typeParameterCon
declare let value: "123";
set(obj, ['a', 'b', 'c'], value);
// Repro from #52007
declare function inners<const T extends readonly any[]>(...args: readonly [unknown, ...T, unknown]): T;
const test = inners(1,2,3,4,5);
declare function inners2<const T extends readonly any[]>(args: readonly [unknown, ...T, unknown]): T;
const test2 = inners2([1,2,3,4,5]);
@@ -71,6 +71,16 @@ declare let obj: Obj;
declare let value: "123";
set(obj, ['a', 'b', 'c'], value);
// Repro from #52007
declare function inners<const T extends readonly any[]>(...args: readonly [unknown, ...T, unknown]): T;
const test = inners(1,2,3,4,5);
declare function inners2<const T extends readonly any[]>(args: readonly [unknown, ...T, unknown]): T;
const test2 = inners2([1,2,3,4,5]);
//// [typeParameterConstModifiers.js]
@@ -105,3 +115,5 @@ var fx1 = function (x) { return x; };
var fx2 = function (x) { return x; };
function set(obj, path, value) { }
set(obj, ['a', 'b', 'c'], value);
var test = inners(1, 2, 3, 4, 5);
var test2 = inners2([1, 2, 3, 4, 5]);
@@ -272,3 +272,27 @@ set(obj, ['a', 'b', 'c'], value);
>obj : Symbol(obj, Decl(typeParameterConstModifiers.ts, 68, 11))
>value : Symbol(value, Decl(typeParameterConstModifiers.ts, 69, 11))
// Repro from #52007
declare function inners<const T extends readonly any[]>(...args: readonly [unknown, ...T, unknown]): T;
>inners : Symbol(inners, Decl(typeParameterConstModifiers.ts, 71, 33))
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 75, 24))
>args : Symbol(args, Decl(typeParameterConstModifiers.ts, 75, 56))
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 75, 24))
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 75, 24))
const test = inners(1,2,3,4,5);
>test : Symbol(test, Decl(typeParameterConstModifiers.ts, 77, 5))
>inners : Symbol(inners, Decl(typeParameterConstModifiers.ts, 71, 33))
declare function inners2<const T extends readonly any[]>(args: readonly [unknown, ...T, unknown]): T;
>inners2 : Symbol(inners2, Decl(typeParameterConstModifiers.ts, 77, 31))
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 79, 25))
>args : Symbol(args, Decl(typeParameterConstModifiers.ts, 79, 57))
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 79, 25))
>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 79, 25))
const test2 = inners2([1,2,3,4,5]);
>test2 : Symbol(test2, Decl(typeParameterConstModifiers.ts, 81, 5))
>inners2 : Symbol(inners2, Decl(typeParameterConstModifiers.ts, 77, 31))
@@ -300,3 +300,34 @@ set(obj, ['a', 'b', 'c'], value);
>'c' : "c"
>value : "123"
// Repro from #52007
declare function inners<const T extends readonly any[]>(...args: readonly [unknown, ...T, unknown]): T;
>inners : <const T extends readonly any[]>(...args: readonly [unknown, ...T, unknown]) => T
>args : readonly [unknown, ...T, unknown]
const test = inners(1,2,3,4,5);
>test : [2, 3, 4]
>inners(1,2,3,4,5) : [2, 3, 4]
>inners : <const T extends readonly any[]>(...args: readonly [unknown, ...T, unknown]) => T
>1 : 1
>2 : 2
>3 : 3
>4 : 4
>5 : 5
declare function inners2<const T extends readonly any[]>(args: readonly [unknown, ...T, unknown]): T;
>inners2 : <const T extends readonly any[]>(args: readonly [unknown, ...T, unknown]) => T
>args : readonly [unknown, ...T, unknown]
const test2 = inners2([1,2,3,4,5]);
>test2 : [2, 3, 4]
>inners2([1,2,3,4,5]) : [2, 3, 4]
>inners2 : <const T extends readonly any[]>(args: readonly [unknown, ...T, unknown]) => T
>[1,2,3,4,5] : [number, 2, 3, 4, number]
>1 : 1
>2 : 2
>3 : 3
>4 : 4
>5 : 5
@@ -72,3 +72,13 @@ declare let obj: Obj;
declare let value: "123";
set(obj, ['a', 'b', 'c'], value);
// Repro from #52007
declare function inners<const T extends readonly any[]>(...args: readonly [unknown, ...T, unknown]): T;
const test = inners(1,2,3,4,5);
declare function inners2<const T extends readonly any[]>(args: readonly [unknown, ...T, unknown]): T;
const test2 = inners2([1,2,3,4,5]);