Skip to content

Commit e24c6f6

Browse files
committed
Add regression tests
1 parent d347d87 commit e24c6f6

4 files changed

+448
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
//// [mappedTypeGenericIndexedAccess.ts]
2+
// Repro from #49242
3+
4+
type Types = {
5+
first: { a1: true };
6+
second: { a2: true };
7+
third: { a3: true };
8+
}
9+
10+
class Test {
11+
entries: { [T in keyof Types]?: Types[T][] };
12+
13+
constructor() {
14+
this.entries = {};
15+
}
16+
17+
addEntry<T extends keyof Types>(name: T, entry: Types[T]) {
18+
if (!this.entries[name]) {
19+
this.entries[name] = [];
20+
}
21+
this.entries[name]?.push(entry);
22+
}
23+
}
24+
25+
// Repro from #49338
26+
27+
type TypesMap = {
28+
[0]: { foo: 'bar'; };
29+
[1]: { a: 'b'; };
30+
};
31+
32+
type P<T extends keyof TypesMap> = { t: T; } & TypesMap[T];
33+
34+
type TypeHandlers = {
35+
[T in keyof TypesMap]?: (p: P<T>) => void;
36+
};
37+
38+
const typeHandlers: TypeHandlers = {
39+
[0]: (p) => console.log(p.foo),
40+
[1]: (p) => console.log(p.a),
41+
};
42+
43+
const onSomeEvent = <T extends keyof TypesMap>(p: P<T>) =>
44+
typeHandlers[p.t]?.(p);
45+
46+
47+
//// [mappedTypeGenericIndexedAccess.js]
48+
"use strict";
49+
// Repro from #49242
50+
var _a;
51+
var Test = /** @class */ (function () {
52+
function Test() {
53+
this.entries = {};
54+
}
55+
Test.prototype.addEntry = function (name, entry) {
56+
var _a;
57+
if (!this.entries[name]) {
58+
this.entries[name] = [];
59+
}
60+
(_a = this.entries[name]) === null || _a === void 0 ? void 0 : _a.push(entry);
61+
};
62+
return Test;
63+
}());
64+
var typeHandlers = (_a = {},
65+
_a[0] = function (p) { return console.log(p.foo); },
66+
_a[1] = function (p) { return console.log(p.a); },
67+
_a);
68+
var onSomeEvent = function (p) { var _a; return (_a = typeHandlers[p.t]) === null || _a === void 0 ? void 0 : _a.call(typeHandlers, p); };
69+
70+
71+
//// [mappedTypeGenericIndexedAccess.d.ts]
72+
declare type Types = {
73+
first: {
74+
a1: true;
75+
};
76+
second: {
77+
a2: true;
78+
};
79+
third: {
80+
a3: true;
81+
};
82+
};
83+
declare class Test {
84+
entries: {
85+
[T in keyof Types]?: Types[T][];
86+
};
87+
constructor();
88+
addEntry<T extends keyof Types>(name: T, entry: Types[T]): void;
89+
}
90+
declare type TypesMap = {
91+
[0]: {
92+
foo: 'bar';
93+
};
94+
[1]: {
95+
a: 'b';
96+
};
97+
};
98+
declare type P<T extends keyof TypesMap> = {
99+
t: T;
100+
} & TypesMap[T];
101+
declare type TypeHandlers = {
102+
[T in keyof TypesMap]?: (p: P<T>) => void;
103+
};
104+
declare const typeHandlers: TypeHandlers;
105+
declare const onSomeEvent: <T extends keyof TypesMap>(p: P<T>) => void | undefined;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
=== tests/cases/compiler/mappedTypeGenericIndexedAccess.ts ===
2+
// Repro from #49242
3+
4+
type Types = {
5+
>Types : Symbol(Types, Decl(mappedTypeGenericIndexedAccess.ts, 0, 0))
6+
7+
first: { a1: true };
8+
>first : Symbol(first, Decl(mappedTypeGenericIndexedAccess.ts, 2, 14))
9+
>a1 : Symbol(a1, Decl(mappedTypeGenericIndexedAccess.ts, 3, 12))
10+
11+
second: { a2: true };
12+
>second : Symbol(second, Decl(mappedTypeGenericIndexedAccess.ts, 3, 24))
13+
>a2 : Symbol(a2, Decl(mappedTypeGenericIndexedAccess.ts, 4, 13))
14+
15+
third: { a3: true };
16+
>third : Symbol(third, Decl(mappedTypeGenericIndexedAccess.ts, 4, 25))
17+
>a3 : Symbol(a3, Decl(mappedTypeGenericIndexedAccess.ts, 5, 12))
18+
}
19+
20+
class Test {
21+
>Test : Symbol(Test, Decl(mappedTypeGenericIndexedAccess.ts, 6, 1))
22+
23+
entries: { [T in keyof Types]?: Types[T][] };
24+
>entries : Symbol(Test.entries, Decl(mappedTypeGenericIndexedAccess.ts, 8, 12))
25+
>T : Symbol(T, Decl(mappedTypeGenericIndexedAccess.ts, 9, 16))
26+
>Types : Symbol(Types, Decl(mappedTypeGenericIndexedAccess.ts, 0, 0))
27+
>Types : Symbol(Types, Decl(mappedTypeGenericIndexedAccess.ts, 0, 0))
28+
>T : Symbol(T, Decl(mappedTypeGenericIndexedAccess.ts, 9, 16))
29+
30+
constructor() {
31+
this.entries = {};
32+
>this.entries : Symbol(Test.entries, Decl(mappedTypeGenericIndexedAccess.ts, 8, 12))
33+
>this : Symbol(Test, Decl(mappedTypeGenericIndexedAccess.ts, 6, 1))
34+
>entries : Symbol(Test.entries, Decl(mappedTypeGenericIndexedAccess.ts, 8, 12))
35+
}
36+
37+
addEntry<T extends keyof Types>(name: T, entry: Types[T]) {
38+
>addEntry : Symbol(Test.addEntry, Decl(mappedTypeGenericIndexedAccess.ts, 13, 5))
39+
>T : Symbol(T, Decl(mappedTypeGenericIndexedAccess.ts, 15, 13))
40+
>Types : Symbol(Types, Decl(mappedTypeGenericIndexedAccess.ts, 0, 0))
41+
>name : Symbol(name, Decl(mappedTypeGenericIndexedAccess.ts, 15, 36))
42+
>T : Symbol(T, Decl(mappedTypeGenericIndexedAccess.ts, 15, 13))
43+
>entry : Symbol(entry, Decl(mappedTypeGenericIndexedAccess.ts, 15, 44))
44+
>Types : Symbol(Types, Decl(mappedTypeGenericIndexedAccess.ts, 0, 0))
45+
>T : Symbol(T, Decl(mappedTypeGenericIndexedAccess.ts, 15, 13))
46+
47+
if (!this.entries[name]) {
48+
>this.entries : Symbol(Test.entries, Decl(mappedTypeGenericIndexedAccess.ts, 8, 12))
49+
>this : Symbol(Test, Decl(mappedTypeGenericIndexedAccess.ts, 6, 1))
50+
>entries : Symbol(Test.entries, Decl(mappedTypeGenericIndexedAccess.ts, 8, 12))
51+
>name : Symbol(name, Decl(mappedTypeGenericIndexedAccess.ts, 15, 36))
52+
53+
this.entries[name] = [];
54+
>this.entries : Symbol(Test.entries, Decl(mappedTypeGenericIndexedAccess.ts, 8, 12))
55+
>this : Symbol(Test, Decl(mappedTypeGenericIndexedAccess.ts, 6, 1))
56+
>entries : Symbol(Test.entries, Decl(mappedTypeGenericIndexedAccess.ts, 8, 12))
57+
>name : Symbol(name, Decl(mappedTypeGenericIndexedAccess.ts, 15, 36))
58+
}
59+
this.entries[name]?.push(entry);
60+
>this.entries[name]?.push : Symbol(Array.push, Decl(lib.es5.d.ts, --, --))
61+
>this.entries : Symbol(Test.entries, Decl(mappedTypeGenericIndexedAccess.ts, 8, 12))
62+
>this : Symbol(Test, Decl(mappedTypeGenericIndexedAccess.ts, 6, 1))
63+
>entries : Symbol(Test.entries, Decl(mappedTypeGenericIndexedAccess.ts, 8, 12))
64+
>name : Symbol(name, Decl(mappedTypeGenericIndexedAccess.ts, 15, 36))
65+
>push : Symbol(Array.push, Decl(lib.es5.d.ts, --, --))
66+
>entry : Symbol(entry, Decl(mappedTypeGenericIndexedAccess.ts, 15, 44))
67+
}
68+
}
69+
70+
// Repro from #49338
71+
72+
type TypesMap = {
73+
>TypesMap : Symbol(TypesMap, Decl(mappedTypeGenericIndexedAccess.ts, 21, 1))
74+
75+
[0]: { foo: 'bar'; };
76+
>[0] : Symbol([0], Decl(mappedTypeGenericIndexedAccess.ts, 25, 17))
77+
>0 : Symbol([0], Decl(mappedTypeGenericIndexedAccess.ts, 25, 17))
78+
>foo : Symbol(foo, Decl(mappedTypeGenericIndexedAccess.ts, 26, 10))
79+
80+
[1]: { a: 'b'; };
81+
>[1] : Symbol([1], Decl(mappedTypeGenericIndexedAccess.ts, 26, 25))
82+
>1 : Symbol([1], Decl(mappedTypeGenericIndexedAccess.ts, 26, 25))
83+
>a : Symbol(a, Decl(mappedTypeGenericIndexedAccess.ts, 27, 10))
84+
85+
};
86+
87+
type P<T extends keyof TypesMap> = { t: T; } & TypesMap[T];
88+
>P : Symbol(P, Decl(mappedTypeGenericIndexedAccess.ts, 28, 2))
89+
>T : Symbol(T, Decl(mappedTypeGenericIndexedAccess.ts, 30, 7))
90+
>TypesMap : Symbol(TypesMap, Decl(mappedTypeGenericIndexedAccess.ts, 21, 1))
91+
>t : Symbol(t, Decl(mappedTypeGenericIndexedAccess.ts, 30, 36))
92+
>T : Symbol(T, Decl(mappedTypeGenericIndexedAccess.ts, 30, 7))
93+
>TypesMap : Symbol(TypesMap, Decl(mappedTypeGenericIndexedAccess.ts, 21, 1))
94+
>T : Symbol(T, Decl(mappedTypeGenericIndexedAccess.ts, 30, 7))
95+
96+
type TypeHandlers = {
97+
>TypeHandlers : Symbol(TypeHandlers, Decl(mappedTypeGenericIndexedAccess.ts, 30, 59))
98+
99+
[T in keyof TypesMap]?: (p: P<T>) => void;
100+
>T : Symbol(T, Decl(mappedTypeGenericIndexedAccess.ts, 33, 5))
101+
>TypesMap : Symbol(TypesMap, Decl(mappedTypeGenericIndexedAccess.ts, 21, 1))
102+
>p : Symbol(p, Decl(mappedTypeGenericIndexedAccess.ts, 33, 29))
103+
>P : Symbol(P, Decl(mappedTypeGenericIndexedAccess.ts, 28, 2))
104+
>T : Symbol(T, Decl(mappedTypeGenericIndexedAccess.ts, 33, 5))
105+
106+
};
107+
108+
const typeHandlers: TypeHandlers = {
109+
>typeHandlers : Symbol(typeHandlers, Decl(mappedTypeGenericIndexedAccess.ts, 36, 5))
110+
>TypeHandlers : Symbol(TypeHandlers, Decl(mappedTypeGenericIndexedAccess.ts, 30, 59))
111+
112+
[0]: (p) => console.log(p.foo),
113+
>[0] : Symbol([0], Decl(mappedTypeGenericIndexedAccess.ts, 36, 36))
114+
>0 : Symbol([0], Decl(mappedTypeGenericIndexedAccess.ts, 36, 36))
115+
>p : Symbol(p, Decl(mappedTypeGenericIndexedAccess.ts, 37, 10))
116+
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
117+
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
118+
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
119+
>p.foo : Symbol(foo, Decl(mappedTypeGenericIndexedAccess.ts, 26, 10))
120+
>p : Symbol(p, Decl(mappedTypeGenericIndexedAccess.ts, 37, 10))
121+
>foo : Symbol(foo, Decl(mappedTypeGenericIndexedAccess.ts, 26, 10))
122+
123+
[1]: (p) => console.log(p.a),
124+
>[1] : Symbol([1], Decl(mappedTypeGenericIndexedAccess.ts, 37, 35))
125+
>1 : Symbol([1], Decl(mappedTypeGenericIndexedAccess.ts, 37, 35))
126+
>p : Symbol(p, Decl(mappedTypeGenericIndexedAccess.ts, 38, 10))
127+
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
128+
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
129+
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
130+
>p.a : Symbol(a, Decl(mappedTypeGenericIndexedAccess.ts, 27, 10))
131+
>p : Symbol(p, Decl(mappedTypeGenericIndexedAccess.ts, 38, 10))
132+
>a : Symbol(a, Decl(mappedTypeGenericIndexedAccess.ts, 27, 10))
133+
134+
};
135+
136+
const onSomeEvent = <T extends keyof TypesMap>(p: P<T>) =>
137+
>onSomeEvent : Symbol(onSomeEvent, Decl(mappedTypeGenericIndexedAccess.ts, 41, 5))
138+
>T : Symbol(T, Decl(mappedTypeGenericIndexedAccess.ts, 41, 21))
139+
>TypesMap : Symbol(TypesMap, Decl(mappedTypeGenericIndexedAccess.ts, 21, 1))
140+
>p : Symbol(p, Decl(mappedTypeGenericIndexedAccess.ts, 41, 47))
141+
>P : Symbol(P, Decl(mappedTypeGenericIndexedAccess.ts, 28, 2))
142+
>T : Symbol(T, Decl(mappedTypeGenericIndexedAccess.ts, 41, 21))
143+
144+
typeHandlers[p.t]?.(p);
145+
>typeHandlers : Symbol(typeHandlers, Decl(mappedTypeGenericIndexedAccess.ts, 36, 5))
146+
>p.t : Symbol(t, Decl(mappedTypeGenericIndexedAccess.ts, 30, 36))
147+
>p : Symbol(p, Decl(mappedTypeGenericIndexedAccess.ts, 41, 47))
148+
>t : Symbol(t, Decl(mappedTypeGenericIndexedAccess.ts, 30, 36))
149+
>p : Symbol(p, Decl(mappedTypeGenericIndexedAccess.ts, 41, 47))
150+

0 commit comments

Comments
 (0)