diff --git a/tests/baselines/reference/typeParameterConstModifiers.errors.txt b/tests/baselines/reference/typeParameterConstModifiers.errors.txt new file mode 100644 index 00000000000..9d42083957b --- /dev/null +++ b/tests/baselines/reference/typeParameterConstModifiers.errors.txt @@ -0,0 +1,68 @@ +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 +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 + + +==== tests/cases/conformance/types/typeParameters/typeParameterLists/typeParameterConstModifiers.ts (2 errors) ==== + declare function f1(x: T): T; + + const x11 = f1('a'); + const x12 = f1(['a', ['b', 'c']]); + const x13 = f1({ a: 1, b: "c", d: ["e", 2, true, { f: "g" }] }); + + declare function f2(x: T | undefined): T; + + const x21 = f2('a'); + const x22 = f2(['a', ['b', 'c']]); + const x23 = f2({ a: 1, b: "c", d: ["e", 2, true, { f: "g" }] }); + + declare function f3(x: T): T[]; + + const x31 = f3("hello"); + const x32 = f3("hello"); + + declare function f4(obj: [T, T]): T; + + const x41 = f4([[1, 'x'], [2, 'y']]); + const x42 = f4([{ a: 1, b: 'x' }, { a: 2, b: 'y' }]); + + declare function f5(obj: { x: T, y: T }): T; + + const x51 = f5({ x: [1, 'x'], y: [2, 'y'] }); + const x52 = f5({ x: { a: 1, b: 'x' }, y: { a: 2, b: 'y' } }); + + declare function f6(...args: T): T; + + const x61 = f6(1, 'b', { a: 1, b: 'x' }); + + class C1 { + constructor(x: T) {} + foo(x: U) { return x; } + } + + const c71 = new C1({ a: 1, b: "c", d: ["e", 2, true, { f: "g" }] }); + const c72 = c71.foo(['a', ['b', 'c']]); + + interface I1 { x: T } // Error + ~~~~~ +!!! error TS1277: 'const' modifier can only appear on a type parameter of a function, method or class + + type T1 = T; // Error + ~~~~~ +!!! error TS1277: 'const' modifier can only appear on a type parameter of a function, method or class + + // Corrected repro from #51745 + + type Obj = { a: { b: { c: "123" } } }; + + type GetPath = + P extends readonly [] ? T : + P extends readonly [infer A extends keyof T, ...infer Rest] ? GetPath : + never; + + function set(obj: T, path: P, value: GetPath) {} + + declare let obj: Obj; + declare let value: "123"; + + set(obj, ['a', 'b', 'c'], value); + \ No newline at end of file diff --git a/tests/baselines/reference/typeParameterConstModifiers.js b/tests/baselines/reference/typeParameterConstModifiers.js new file mode 100644 index 00000000000..b67c25273cf --- /dev/null +++ b/tests/baselines/reference/typeParameterConstModifiers.js @@ -0,0 +1,86 @@ +//// [typeParameterConstModifiers.ts] +declare function f1(x: T): T; + +const x11 = f1('a'); +const x12 = f1(['a', ['b', 'c']]); +const x13 = f1({ a: 1, b: "c", d: ["e", 2, true, { f: "g" }] }); + +declare function f2(x: T | undefined): T; + +const x21 = f2('a'); +const x22 = f2(['a', ['b', 'c']]); +const x23 = f2({ a: 1, b: "c", d: ["e", 2, true, { f: "g" }] }); + +declare function f3(x: T): T[]; + +const x31 = f3("hello"); +const x32 = f3("hello"); + +declare function f4(obj: [T, T]): T; + +const x41 = f4([[1, 'x'], [2, 'y']]); +const x42 = f4([{ a: 1, b: 'x' }, { a: 2, b: 'y' }]); + +declare function f5(obj: { x: T, y: T }): T; + +const x51 = f5({ x: [1, 'x'], y: [2, 'y'] }); +const x52 = f5({ x: { a: 1, b: 'x' }, y: { a: 2, b: 'y' } }); + +declare function f6(...args: T): T; + +const x61 = f6(1, 'b', { a: 1, b: 'x' }); + +class C1 { + constructor(x: T) {} + foo(x: U) { return x; } +} + +const c71 = new C1({ a: 1, b: "c", d: ["e", 2, true, { f: "g" }] }); +const c72 = c71.foo(['a', ['b', 'c']]); + +interface I1 { x: T } // Error + +type T1 = T; // Error + +// Corrected repro from #51745 + +type Obj = { a: { b: { c: "123" } } }; + +type GetPath = + P extends readonly [] ? T : + P extends readonly [infer A extends keyof T, ...infer Rest] ? GetPath : + never; + +function set(obj: T, path: P, value: GetPath) {} + +declare let obj: Obj; +declare let value: "123"; + +set(obj, ['a', 'b', 'c'], value); + + +//// [typeParameterConstModifiers.js] +"use strict"; +var x11 = f1('a'); +var x12 = f1(['a', ['b', 'c']]); +var x13 = f1({ a: 1, b: "c", d: ["e", 2, true, { f: "g" }] }); +var x21 = f2('a'); +var x22 = f2(['a', ['b', 'c']]); +var x23 = f2({ a: 1, b: "c", d: ["e", 2, true, { f: "g" }] }); +var x31 = f3("hello"); +var x32 = f3("hello"); +var x41 = f4([[1, 'x'], [2, 'y']]); +var x42 = f4([{ a: 1, b: 'x' }, { a: 2, b: 'y' }]); +var x51 = f5({ x: [1, 'x'], y: [2, 'y'] }); +var x52 = f5({ x: { a: 1, b: 'x' }, y: { a: 2, b: 'y' } }); +var x61 = f6(1, 'b', { a: 1, b: 'x' }); +var C1 = /** @class */ (function () { + function C1(x) { + } + C1.prototype.foo = function (x) { return x; }; + return C1; +}()); +var c71 = new C1({ a: 1, b: "c", d: ["e", 2, true, { f: "g" }] }); +var c72 = c71.foo(['a', ['b', 'c']]); +function set(obj, path, value) { } +set(obj, ['a', 'b', 'c'], value); diff --git a/tests/baselines/reference/typeParameterConstModifiers.symbols b/tests/baselines/reference/typeParameterConstModifiers.symbols new file mode 100644 index 00000000000..628558a4be1 --- /dev/null +++ b/tests/baselines/reference/typeParameterConstModifiers.symbols @@ -0,0 +1,217 @@ +=== tests/cases/conformance/types/typeParameters/typeParameterLists/typeParameterConstModifiers.ts === +declare function f1(x: T): T; +>f1 : Symbol(f1, Decl(typeParameterConstModifiers.ts, 0, 0)) +>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 0, 20)) +>x : Symbol(x, Decl(typeParameterConstModifiers.ts, 0, 29)) +>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 0, 20)) +>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 0, 20)) + +const x11 = f1('a'); +>x11 : Symbol(x11, Decl(typeParameterConstModifiers.ts, 2, 5)) +>f1 : Symbol(f1, Decl(typeParameterConstModifiers.ts, 0, 0)) + +const x12 = f1(['a', ['b', 'c']]); +>x12 : Symbol(x12, Decl(typeParameterConstModifiers.ts, 3, 5)) +>f1 : Symbol(f1, Decl(typeParameterConstModifiers.ts, 0, 0)) + +const x13 = f1({ a: 1, b: "c", d: ["e", 2, true, { f: "g" }] }); +>x13 : Symbol(x13, Decl(typeParameterConstModifiers.ts, 4, 5)) +>f1 : Symbol(f1, Decl(typeParameterConstModifiers.ts, 0, 0)) +>a : Symbol(a, Decl(typeParameterConstModifiers.ts, 4, 16)) +>b : Symbol(b, Decl(typeParameterConstModifiers.ts, 4, 22)) +>d : Symbol(d, Decl(typeParameterConstModifiers.ts, 4, 30)) +>f : Symbol(f, Decl(typeParameterConstModifiers.ts, 4, 50)) + +declare function f2(x: T | undefined): T; +>f2 : Symbol(f2, Decl(typeParameterConstModifiers.ts, 4, 64)) +>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 6, 20)) +>U : Symbol(U, Decl(typeParameterConstModifiers.ts, 6, 28)) +>x : Symbol(x, Decl(typeParameterConstModifiers.ts, 6, 32)) +>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 6, 20)) +>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 6, 20)) + +const x21 = f2('a'); +>x21 : Symbol(x21, Decl(typeParameterConstModifiers.ts, 8, 5)) +>f2 : Symbol(f2, Decl(typeParameterConstModifiers.ts, 4, 64)) + +const x22 = f2(['a', ['b', 'c']]); +>x22 : Symbol(x22, Decl(typeParameterConstModifiers.ts, 9, 5)) +>f2 : Symbol(f2, Decl(typeParameterConstModifiers.ts, 4, 64)) + +const x23 = f2({ a: 1, b: "c", d: ["e", 2, true, { f: "g" }] }); +>x23 : Symbol(x23, Decl(typeParameterConstModifiers.ts, 10, 5)) +>f2 : Symbol(f2, Decl(typeParameterConstModifiers.ts, 4, 64)) +>a : Symbol(a, Decl(typeParameterConstModifiers.ts, 10, 16)) +>b : Symbol(b, Decl(typeParameterConstModifiers.ts, 10, 22)) +>d : Symbol(d, Decl(typeParameterConstModifiers.ts, 10, 30)) +>f : Symbol(f, Decl(typeParameterConstModifiers.ts, 10, 50)) + +declare function f3(x: T): T[]; +>f3 : Symbol(f3, Decl(typeParameterConstModifiers.ts, 10, 64)) +>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 12, 20)) +>x : Symbol(x, Decl(typeParameterConstModifiers.ts, 12, 29)) +>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 12, 20)) +>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 12, 20)) + +const x31 = f3("hello"); +>x31 : Symbol(x31, Decl(typeParameterConstModifiers.ts, 14, 5)) +>f3 : Symbol(f3, Decl(typeParameterConstModifiers.ts, 10, 64)) + +const x32 = f3("hello"); +>x32 : Symbol(x32, Decl(typeParameterConstModifiers.ts, 15, 5)) +>f3 : Symbol(f3, Decl(typeParameterConstModifiers.ts, 10, 64)) + +declare function f4(obj: [T, T]): T; +>f4 : Symbol(f4, Decl(typeParameterConstModifiers.ts, 15, 24)) +>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 17, 20)) +>obj : Symbol(obj, Decl(typeParameterConstModifiers.ts, 17, 29)) +>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 17, 20)) +>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 17, 20)) +>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 17, 20)) + +const x41 = f4([[1, 'x'], [2, 'y']]); +>x41 : Symbol(x41, Decl(typeParameterConstModifiers.ts, 19, 5)) +>f4 : Symbol(f4, Decl(typeParameterConstModifiers.ts, 15, 24)) + +const x42 = f4([{ a: 1, b: 'x' }, { a: 2, b: 'y' }]); +>x42 : Symbol(x42, Decl(typeParameterConstModifiers.ts, 20, 5)) +>f4 : Symbol(f4, Decl(typeParameterConstModifiers.ts, 15, 24)) +>a : Symbol(a, Decl(typeParameterConstModifiers.ts, 20, 17)) +>b : Symbol(b, Decl(typeParameterConstModifiers.ts, 20, 23)) +>a : Symbol(a, Decl(typeParameterConstModifiers.ts, 20, 35)) +>b : Symbol(b, Decl(typeParameterConstModifiers.ts, 20, 41)) + +declare function f5(obj: { x: T, y: T }): T; +>f5 : Symbol(f5, Decl(typeParameterConstModifiers.ts, 20, 53)) +>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 22, 20)) +>obj : Symbol(obj, Decl(typeParameterConstModifiers.ts, 22, 29)) +>x : Symbol(x, Decl(typeParameterConstModifiers.ts, 22, 35)) +>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 22, 20)) +>y : Symbol(y, Decl(typeParameterConstModifiers.ts, 22, 41)) +>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 22, 20)) +>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 22, 20)) + +const x51 = f5({ x: [1, 'x'], y: [2, 'y'] }); +>x51 : Symbol(x51, Decl(typeParameterConstModifiers.ts, 24, 5)) +>f5 : Symbol(f5, Decl(typeParameterConstModifiers.ts, 20, 53)) +>x : Symbol(x, Decl(typeParameterConstModifiers.ts, 24, 16)) +>y : Symbol(y, Decl(typeParameterConstModifiers.ts, 24, 29)) + +const x52 = f5({ x: { a: 1, b: 'x' }, y: { a: 2, b: 'y' } }); +>x52 : Symbol(x52, Decl(typeParameterConstModifiers.ts, 25, 5)) +>f5 : Symbol(f5, Decl(typeParameterConstModifiers.ts, 20, 53)) +>x : Symbol(x, Decl(typeParameterConstModifiers.ts, 25, 16)) +>a : Symbol(a, Decl(typeParameterConstModifiers.ts, 25, 21)) +>b : Symbol(b, Decl(typeParameterConstModifiers.ts, 25, 27)) +>y : Symbol(y, Decl(typeParameterConstModifiers.ts, 25, 37)) +>a : Symbol(a, Decl(typeParameterConstModifiers.ts, 25, 42)) +>b : Symbol(b, Decl(typeParameterConstModifiers.ts, 25, 48)) + +declare function f6(...args: T): T; +>f6 : Symbol(f6, Decl(typeParameterConstModifiers.ts, 25, 61)) +>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 27, 20)) +>args : Symbol(args, Decl(typeParameterConstModifiers.ts, 27, 56)) +>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 27, 20)) +>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 27, 20)) + +const x61 = f6(1, 'b', { a: 1, b: 'x' }); +>x61 : Symbol(x61, Decl(typeParameterConstModifiers.ts, 29, 5)) +>f6 : Symbol(f6, Decl(typeParameterConstModifiers.ts, 25, 61)) +>a : Symbol(a, Decl(typeParameterConstModifiers.ts, 29, 24)) +>b : Symbol(b, Decl(typeParameterConstModifiers.ts, 29, 30)) + +class C1 { +>C1 : Symbol(C1, Decl(typeParameterConstModifiers.ts, 29, 41)) +>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 31, 9)) + + constructor(x: T) {} +>x : Symbol(x, Decl(typeParameterConstModifiers.ts, 32, 16)) +>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 31, 9)) + + foo(x: U) { return x; } +>foo : Symbol(C1.foo, Decl(typeParameterConstModifiers.ts, 32, 24)) +>U : Symbol(U, Decl(typeParameterConstModifiers.ts, 33, 8)) +>x : Symbol(x, Decl(typeParameterConstModifiers.ts, 33, 17)) +>U : Symbol(U, Decl(typeParameterConstModifiers.ts, 33, 8)) +>x : Symbol(x, Decl(typeParameterConstModifiers.ts, 33, 17)) +} + +const c71 = new C1({ a: 1, b: "c", d: ["e", 2, true, { f: "g" }] }); +>c71 : Symbol(c71, Decl(typeParameterConstModifiers.ts, 36, 5)) +>C1 : Symbol(C1, Decl(typeParameterConstModifiers.ts, 29, 41)) +>a : Symbol(a, Decl(typeParameterConstModifiers.ts, 36, 20)) +>b : Symbol(b, Decl(typeParameterConstModifiers.ts, 36, 26)) +>d : Symbol(d, Decl(typeParameterConstModifiers.ts, 36, 34)) +>f : Symbol(f, Decl(typeParameterConstModifiers.ts, 36, 54)) + +const c72 = c71.foo(['a', ['b', 'c']]); +>c72 : Symbol(c72, Decl(typeParameterConstModifiers.ts, 37, 5)) +>c71.foo : Symbol(C1.foo, Decl(typeParameterConstModifiers.ts, 32, 24)) +>c71 : Symbol(c71, Decl(typeParameterConstModifiers.ts, 36, 5)) +>foo : Symbol(C1.foo, Decl(typeParameterConstModifiers.ts, 32, 24)) + +interface I1 { x: T } // Error +>I1 : Symbol(I1, Decl(typeParameterConstModifiers.ts, 37, 39)) +>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 39, 13)) +>x : Symbol(I1.x, Decl(typeParameterConstModifiers.ts, 39, 23)) +>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 39, 13)) + +type T1 = T; // Error +>T1 : Symbol(T1, Decl(typeParameterConstModifiers.ts, 39, 30)) +>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 41, 8)) +>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 41, 8)) + +// Corrected repro from #51745 + +type Obj = { a: { b: { c: "123" } } }; +>Obj : Symbol(Obj, Decl(typeParameterConstModifiers.ts, 41, 21)) +>a : Symbol(a, Decl(typeParameterConstModifiers.ts, 45, 12)) +>b : Symbol(b, Decl(typeParameterConstModifiers.ts, 45, 17)) +>c : Symbol(c, Decl(typeParameterConstModifiers.ts, 45, 22)) + +type GetPath = +>GetPath : Symbol(GetPath, Decl(typeParameterConstModifiers.ts, 45, 38)) +>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 47, 13)) +>P : Symbol(P, Decl(typeParameterConstModifiers.ts, 47, 15)) + + P extends readonly [] ? T : +>P : Symbol(P, Decl(typeParameterConstModifiers.ts, 47, 15)) +>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 47, 13)) + + P extends readonly [infer A extends keyof T, ...infer Rest] ? GetPath : +>P : Symbol(P, Decl(typeParameterConstModifiers.ts, 47, 15)) +>A : Symbol(A, Decl(typeParameterConstModifiers.ts, 49, 29)) +>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 47, 13)) +>Rest : Symbol(Rest, Decl(typeParameterConstModifiers.ts, 49, 57)) +>GetPath : Symbol(GetPath, Decl(typeParameterConstModifiers.ts, 45, 38)) +>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 47, 13)) +>A : Symbol(A, Decl(typeParameterConstModifiers.ts, 49, 29)) +>Rest : Symbol(Rest, Decl(typeParameterConstModifiers.ts, 49, 57)) + + never; + +function set(obj: T, path: P, value: GetPath) {} +>set : Symbol(set, Decl(typeParameterConstModifiers.ts, 50, 10)) +>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 52, 13)) +>P : Symbol(P, Decl(typeParameterConstModifiers.ts, 52, 15)) +>obj : Symbol(obj, Decl(typeParameterConstModifiers.ts, 52, 51)) +>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 52, 13)) +>path : Symbol(path, Decl(typeParameterConstModifiers.ts, 52, 58)) +>P : Symbol(P, Decl(typeParameterConstModifiers.ts, 52, 15)) +>value : Symbol(value, Decl(typeParameterConstModifiers.ts, 52, 67)) +>GetPath : Symbol(GetPath, Decl(typeParameterConstModifiers.ts, 45, 38)) +>T : Symbol(T, Decl(typeParameterConstModifiers.ts, 52, 13)) +>P : Symbol(P, Decl(typeParameterConstModifiers.ts, 52, 15)) + +declare let obj: Obj; +>obj : Symbol(obj, Decl(typeParameterConstModifiers.ts, 54, 11)) +>Obj : Symbol(Obj, Decl(typeParameterConstModifiers.ts, 41, 21)) + +declare let value: "123"; +>value : Symbol(value, Decl(typeParameterConstModifiers.ts, 55, 11)) + +set(obj, ['a', 'b', 'c'], value); +>set : Symbol(set, Decl(typeParameterConstModifiers.ts, 50, 10)) +>obj : Symbol(obj, Decl(typeParameterConstModifiers.ts, 54, 11)) +>value : Symbol(value, Decl(typeParameterConstModifiers.ts, 55, 11)) + diff --git a/tests/baselines/reference/typeParameterConstModifiers.types b/tests/baselines/reference/typeParameterConstModifiers.types new file mode 100644 index 00000000000..af172314e76 --- /dev/null +++ b/tests/baselines/reference/typeParameterConstModifiers.types @@ -0,0 +1,264 @@ +=== tests/cases/conformance/types/typeParameters/typeParameterLists/typeParameterConstModifiers.ts === +declare function f1(x: T): T; +>f1 : (x: T) => T +>x : T + +const x11 = f1('a'); +>x11 : "a" +>f1('a') : "a" +>f1 : (x: T) => T +>'a' : "a" + +const x12 = f1(['a', ['b', 'c']]); +>x12 : readonly ["a", readonly ["b", "c"]] +>f1(['a', ['b', 'c']]) : readonly ["a", readonly ["b", "c"]] +>f1 : (x: T) => T +>['a', ['b', 'c']] : ["a", ["b", "c"]] +>'a' : "a" +>['b', 'c'] : ["b", "c"] +>'b' : "b" +>'c' : "c" + +const x13 = f1({ a: 1, b: "c", d: ["e", 2, true, { f: "g" }] }); +>x13 : { readonly a: 1; readonly b: "c"; readonly d: readonly ["e", 2, true, { readonly f: "g"; }]; } +>f1({ a: 1, b: "c", d: ["e", 2, true, { f: "g" }] }) : { readonly a: 1; readonly b: "c"; readonly d: readonly ["e", 2, true, { readonly f: "g"; }]; } +>f1 : (x: T) => T +>{ a: 1, b: "c", d: ["e", 2, true, { f: "g" }] } : { a: 1; b: "c"; d: ["e", 2, true, { f: "g"; }]; } +>a : 1 +>1 : 1 +>b : "c" +>"c" : "c" +>d : ["e", 2, true, { f: "g"; }] +>["e", 2, true, { f: "g" }] : ["e", 2, true, { f: "g"; }] +>"e" : "e" +>2 : 2 +>true : true +>{ f: "g" } : { f: "g"; } +>f : "g" +>"g" : "g" + +declare function f2(x: T | undefined): T; +>f2 : (x: T | undefined) => T +>x : T | undefined + +const x21 = f2('a'); +>x21 : "a" +>f2('a') : "a" +>f2 : (x: T | undefined) => T +>'a' : "a" + +const x22 = f2(['a', ['b', 'c']]); +>x22 : readonly ["a", readonly ["b", "c"]] +>f2(['a', ['b', 'c']]) : readonly ["a", readonly ["b", "c"]] +>f2 : (x: T | undefined) => T +>['a', ['b', 'c']] : ["a", ["b", "c"]] +>'a' : "a" +>['b', 'c'] : ["b", "c"] +>'b' : "b" +>'c' : "c" + +const x23 = f2({ a: 1, b: "c", d: ["e", 2, true, { f: "g" }] }); +>x23 : { readonly a: 1; readonly b: "c"; readonly d: readonly ["e", 2, true, { readonly f: "g"; }]; } +>f2({ a: 1, b: "c", d: ["e", 2, true, { f: "g" }] }) : { readonly a: 1; readonly b: "c"; readonly d: readonly ["e", 2, true, { readonly f: "g"; }]; } +>f2 : (x: T | undefined) => T +>{ a: 1, b: "c", d: ["e", 2, true, { f: "g" }] } : { a: 1; b: "c"; d: ["e", 2, true, { f: "g"; }]; } +>a : 1 +>1 : 1 +>b : "c" +>"c" : "c" +>d : ["e", 2, true, { f: "g"; }] +>["e", 2, true, { f: "g" }] : ["e", 2, true, { f: "g"; }] +>"e" : "e" +>2 : 2 +>true : true +>{ f: "g" } : { f: "g"; } +>f : "g" +>"g" : "g" + +declare function f3(x: T): T[]; +>f3 : (x: T) => T[] +>x : T + +const x31 = f3("hello"); +>x31 : "hello"[] +>f3("hello") : "hello"[] +>f3 : (x: T) => T[] +>"hello" : "hello" + +const x32 = f3("hello"); +>x32 : "hello"[] +>f3("hello") : "hello"[] +>f3 : (x: T) => T[] +>"hello" : "hello" + +declare function f4(obj: [T, T]): T; +>f4 : (obj: [T, T]) => T +>obj : [T, T] + +const x41 = f4([[1, 'x'], [2, 'y']]); +>x41 : readonly [1, "x"] | readonly [2, "y"] +>f4([[1, 'x'], [2, 'y']]) : readonly [1, "x"] | readonly [2, "y"] +>f4 : (obj: [T, T]) => T +>[[1, 'x'], [2, 'y']] : [[1, "x"], [2, "y"]] +>[1, 'x'] : [1, "x"] +>1 : 1 +>'x' : "x" +>[2, 'y'] : [2, "y"] +>2 : 2 +>'y' : "y" + +const x42 = f4([{ a: 1, b: 'x' }, { a: 2, b: 'y' }]); +>x42 : { readonly a: 1; readonly b: "x"; } | { readonly a: 2; readonly b: "y"; } +>f4([{ a: 1, b: 'x' }, { a: 2, b: 'y' }]) : { readonly a: 1; readonly b: "x"; } | { readonly a: 2; readonly b: "y"; } +>f4 : (obj: [T, T]) => T +>[{ a: 1, b: 'x' }, { a: 2, b: 'y' }] : [{ a: 1; b: "x"; }, { a: 2; b: "y"; }] +>{ a: 1, b: 'x' } : { a: 1; b: "x"; } +>a : 1 +>1 : 1 +>b : "x" +>'x' : "x" +>{ a: 2, b: 'y' } : { a: 2; b: "y"; } +>a : 2 +>2 : 2 +>b : "y" +>'y' : "y" + +declare function f5(obj: { x: T, y: T }): T; +>f5 : (obj: { x: T; y: T;}) => T +>obj : { x: T; y: T; } +>x : T +>y : T + +const x51 = f5({ x: [1, 'x'], y: [2, 'y'] }); +>x51 : readonly [1, "x"] | readonly [2, "y"] +>f5({ x: [1, 'x'], y: [2, 'y'] }) : readonly [1, "x"] | readonly [2, "y"] +>f5 : (obj: { x: T; y: T; }) => T +>{ x: [1, 'x'], y: [2, 'y'] } : { x: [1, "x"]; y: [2, "y"]; } +>x : [1, "x"] +>[1, 'x'] : [1, "x"] +>1 : 1 +>'x' : "x" +>y : [2, "y"] +>[2, 'y'] : [2, "y"] +>2 : 2 +>'y' : "y" + +const x52 = f5({ x: { a: 1, b: 'x' }, y: { a: 2, b: 'y' } }); +>x52 : { readonly a: 1; readonly b: "x"; } | { readonly a: 2; readonly b: "y"; } +>f5({ x: { a: 1, b: 'x' }, y: { a: 2, b: 'y' } }) : { readonly a: 1; readonly b: "x"; } | { readonly a: 2; readonly b: "y"; } +>f5 : (obj: { x: T; y: T; }) => T +>{ x: { a: 1, b: 'x' }, y: { a: 2, b: 'y' } } : { x: { a: 1; b: "x"; }; y: { a: 2; b: "y"; }; } +>x : { a: 1; b: "x"; } +>{ a: 1, b: 'x' } : { a: 1; b: "x"; } +>a : 1 +>1 : 1 +>b : "x" +>'x' : "x" +>y : { a: 2; b: "y"; } +>{ a: 2, b: 'y' } : { a: 2; b: "y"; } +>a : 2 +>2 : 2 +>b : "y" +>'y' : "y" + +declare function f6(...args: T): T; +>f6 : (...args: T) => T +>args : T + +const x61 = f6(1, 'b', { a: 1, b: 'x' }); +>x61 : readonly [1, "b", { readonly a: 1; readonly b: "x"; }] +>f6(1, 'b', { a: 1, b: 'x' }) : readonly [1, "b", { readonly a: 1; readonly b: "x"; }] +>f6 : (...args: T) => T +>1 : 1 +>'b' : "b" +>{ a: 1, b: 'x' } : { a: 1; b: "x"; } +>a : 1 +>1 : 1 +>b : "x" +>'x' : "x" + +class C1 { +>C1 : C1 + + constructor(x: T) {} +>x : T + + foo(x: U) { return x; } +>foo : (x: U) => U +>x : U +>x : U +} + +const c71 = new C1({ a: 1, b: "c", d: ["e", 2, true, { f: "g" }] }); +>c71 : C1<{ readonly a: 1; readonly b: "c"; readonly d: readonly ["e", 2, true, { readonly f: "g"; }]; }> +>new C1({ a: 1, b: "c", d: ["e", 2, true, { f: "g" }] }) : C1<{ readonly a: 1; readonly b: "c"; readonly d: readonly ["e", 2, true, { readonly f: "g"; }]; }> +>C1 : typeof C1 +>{ a: 1, b: "c", d: ["e", 2, true, { f: "g" }] } : { a: 1; b: "c"; d: ["e", 2, true, { f: "g"; }]; } +>a : 1 +>1 : 1 +>b : "c" +>"c" : "c" +>d : ["e", 2, true, { f: "g"; }] +>["e", 2, true, { f: "g" }] : ["e", 2, true, { f: "g"; }] +>"e" : "e" +>2 : 2 +>true : true +>{ f: "g" } : { f: "g"; } +>f : "g" +>"g" : "g" + +const c72 = c71.foo(['a', ['b', 'c']]); +>c72 : readonly ["a", readonly ["b", "c"]] +>c71.foo(['a', ['b', 'c']]) : readonly ["a", readonly ["b", "c"]] +>c71.foo : (x: U) => U +>c71 : C1<{ readonly a: 1; readonly b: "c"; readonly d: readonly ["e", 2, true, { readonly f: "g"; }]; }> +>foo : (x: U) => U +>['a', ['b', 'c']] : ["a", ["b", "c"]] +>'a' : "a" +>['b', 'c'] : ["b", "c"] +>'b' : "b" +>'c' : "c" + +interface I1 { x: T } // Error +>x : T + +type T1 = T; // Error +>T1 : T + +// Corrected repro from #51745 + +type Obj = { a: { b: { c: "123" } } }; +>Obj : { a: { b: { c: "123"; };}; } +>a : { b: { c: "123";}; } +>b : { c: "123"; } +>c : "123" + +type GetPath = +>GetPath : GetPath + + P extends readonly [] ? T : + P extends readonly [infer A extends keyof T, ...infer Rest] ? GetPath : + never; + +function set(obj: T, path: P, value: GetPath) {} +>set : (obj: T, path: P, value: GetPath) => void +>obj : T +>path : P +>value : GetPath + +declare let obj: Obj; +>obj : Obj + +declare let value: "123"; +>value : "123" + +set(obj, ['a', 'b', 'c'], value); +>set(obj, ['a', 'b', 'c'], value) : void +>set : (obj: T, path: P, value: GetPath) => void +>obj : Obj +>['a', 'b', 'c'] : ["a", "b", "c"] +>'a' : "a" +>'b' : "b" +>'c' : "c" +>value : "123" + diff --git a/tests/cases/conformance/types/typeParameters/typeParameterLists/typeParameterConstModifiers.ts b/tests/cases/conformance/types/typeParameters/typeParameterLists/typeParameterConstModifiers.ts new file mode 100644 index 00000000000..8ce99d87350 --- /dev/null +++ b/tests/cases/conformance/types/typeParameters/typeParameterLists/typeParameterConstModifiers.ts @@ -0,0 +1,60 @@ +// @strict: true + +declare function f1(x: T): T; + +const x11 = f1('a'); +const x12 = f1(['a', ['b', 'c']]); +const x13 = f1({ a: 1, b: "c", d: ["e", 2, true, { f: "g" }] }); + +declare function f2(x: T | undefined): T; + +const x21 = f2('a'); +const x22 = f2(['a', ['b', 'c']]); +const x23 = f2({ a: 1, b: "c", d: ["e", 2, true, { f: "g" }] }); + +declare function f3(x: T): T[]; + +const x31 = f3("hello"); +const x32 = f3("hello"); + +declare function f4(obj: [T, T]): T; + +const x41 = f4([[1, 'x'], [2, 'y']]); +const x42 = f4([{ a: 1, b: 'x' }, { a: 2, b: 'y' }]); + +declare function f5(obj: { x: T, y: T }): T; + +const x51 = f5({ x: [1, 'x'], y: [2, 'y'] }); +const x52 = f5({ x: { a: 1, b: 'x' }, y: { a: 2, b: 'y' } }); + +declare function f6(...args: T): T; + +const x61 = f6(1, 'b', { a: 1, b: 'x' }); + +class C1 { + constructor(x: T) {} + foo(x: U) { return x; } +} + +const c71 = new C1({ a: 1, b: "c", d: ["e", 2, true, { f: "g" }] }); +const c72 = c71.foo(['a', ['b', 'c']]); + +interface I1 { x: T } // Error + +type T1 = T; // Error + +// Corrected repro from #51745 + +type Obj = { a: { b: { c: "123" } } }; + +type GetPath = + P extends readonly [] ? T : + P extends readonly [infer A extends keyof T, ...infer Rest] ? GetPath : + never; + +function set(obj: T, path: P, value: GetPath) {} + +declare let obj: Obj; +declare let value: "123"; + +set(obj, ['a', 'b', 'c'], value);