Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add tests
  • Loading branch information
ahejlsberg committed Dec 12, 2022
commit ccf252f8f3d07d90ca7d25cd62887cdc7fdf5b7a
68 changes: 68 additions & 0 deletions tests/baselines/reference/typeParameterConstModifiers.errors.txt
Original file line number Diff line number Diff line change
@@ -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<const T>(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<const T, U>(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<const T>(x: T): T[];

const x31 = f3("hello");
const x32 = f3("hello");

declare function f4<const T>(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<const T>(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<const T extends readonly unknown[]>(...args: T): T;

const x61 = f6(1, 'b', { a: 1, b: 'x' });

class C1<const T> {
constructor(x: T) {}
foo<const U>(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<const T> { x: T } // Error
~~~~~
!!! error TS1277: 'const' modifier can only appear on a type parameter of a function, method or class

type T1<const T> = 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<T, P> =
P extends readonly [] ? T :
P extends readonly [infer A extends keyof T, ...infer Rest] ? GetPath<T[A], Rest> :
never;

function set<T, const P extends readonly string[]>(obj: T, path: P, value: GetPath<T, P>) {}

declare let obj: Obj;
declare let value: "123";

set(obj, ['a', 'b', 'c'], value);

86 changes: 86 additions & 0 deletions tests/baselines/reference/typeParameterConstModifiers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
//// [typeParameterConstModifiers.ts]
declare function f1<const T>(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<const T, U>(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<const T>(x: T): T[];

const x31 = f3("hello");
const x32 = f3("hello");

declare function f4<const T>(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<const T>(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<const T extends readonly unknown[]>(...args: T): T;

const x61 = f6(1, 'b', { a: 1, b: 'x' });

class C1<const T> {
constructor(x: T) {}
foo<const U>(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<const T> { x: T } // Error

type T1<const T> = T; // Error

// Corrected repro from #51745

type Obj = { a: { b: { c: "123" } } };

type GetPath<T, P> =
P extends readonly [] ? T :
P extends readonly [infer A extends keyof T, ...infer Rest] ? GetPath<T[A], Rest> :
never;

function set<T, const P extends readonly string[]>(obj: T, path: P, value: GetPath<T, P>) {}

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);
217 changes: 217 additions & 0 deletions tests/baselines/reference/typeParameterConstModifiers.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
=== tests/cases/conformance/types/typeParameters/typeParameterLists/typeParameterConstModifiers.ts ===
declare function f1<const T>(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<const T, U>(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<const T>(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<const T>(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<const T>(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<const T extends readonly unknown[]>(...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<const T> {
>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<const U>(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<const T> { 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<const T> = 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<T, P> =
>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<T[A], Rest> :
>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<T, const P extends readonly string[]>(obj: T, path: P, value: GetPath<T, P>) {}
>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))

Loading