Skip to content

Commit 7f49c62

Browse files
committed
Also instantiate indexed accesses
1 parent 35d28d0 commit 7f49c62

File tree

5 files changed

+227
-138
lines changed

5 files changed

+227
-138
lines changed

src/compiler/checker.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -9677,8 +9677,9 @@ namespace ts {
96779677
else if (source.flags & TypeFlags.IndexedAccess) {
96789678
// A type S[K] is related to a type T if A[K] is related to T, where K is string-like and
96799679
// A is the apparent type of S.
9680-
const constraint = getConstraintOfIndexedAccess(<IndexedAccessType>source);
9680+
let constraint = getConstraintOfIndexedAccess(<IndexedAccessType>source);
96819681
if (constraint) {
9682+
constraint = getTypeWithThisArgument(constraint, source);
96829683
if (result = isRelatedTo(constraint, target, reportErrors)) {
96839684
errorInfo = saveErrorInfo;
96849685
return result;
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,41 @@
11
//// [collectionPatternNoError.ts]
22
interface MsgConstructor<T extends Message> {
3-
new(data: Array<{}>): T;
3+
new(data: Array<{}>): T;
4+
}
5+
class Message {
6+
clone(): this {
7+
return this;
48
}
5-
class Message {
6-
clone(): this {
7-
return this;
8-
}
9+
}
10+
interface MessageList<T extends Message> extends Message {
11+
methodOnMessageList(): T[];
12+
}
13+
14+
function fetchMsg<V extends Message>(protoCtor: MsgConstructor<V>): V {
15+
return null!;
16+
}
17+
18+
class DataProvider<T extends Message, U extends MessageList<T>> {
19+
constructor(
20+
private readonly message: MsgConstructor<T>,
21+
private readonly messageList: MsgConstructor<U>,
22+
) { }
23+
24+
fetch() {
25+
const messageList = fetchMsg(this.messageList);
26+
messageList.methodOnMessageList();
927
}
10-
interface MessageList<T extends Message> extends Message {
11-
methodOnMessageList(): T[];
12-
}
13-
14-
function fetchMsg<V extends Message>(protoCtor: MsgConstructor<V>): V {
15-
return null!;
16-
}
17-
18-
class DataProvider<T extends Message, U extends MessageList<T>> {
19-
constructor(
20-
private readonly message: MsgConstructor<T>,
21-
private readonly messageList: MsgConstructor<U>,
22-
) {}
23-
24-
fetch() {
25-
const messageList = fetchMsg(this.messageList);
26-
messageList.methodOnMessageList();
27-
}
28-
}
28+
}
29+
30+
// The same bug as the above but using indexed accesses
31+
// (won't surface directly unless unsound indexed access assignments are forbidden)
32+
function f<
33+
U extends {TType: MessageList<T>},
34+
T extends Message
35+
>(message: MsgConstructor<T>, messageList: MsgConstructor<U["TType"]>) {
36+
fetchMsg(messageList).methodOnMessageList();
37+
}
38+
2939

3040
//// [collectionPatternNoError.js]
3141
var Message = /** @class */ (function () {
@@ -50,3 +60,8 @@ var DataProvider = /** @class */ (function () {
5060
};
5161
return DataProvider;
5262
}());
63+
// The same bug as the above but using indexed accesses
64+
// (won't surface directly unless unsound indexed access assignments are forbidden)
65+
function f(message, messageList) {
66+
fetchMsg(messageList).methodOnMessageList();
67+
}

tests/baselines/reference/collectionPatternNoError.symbols

+96-65
Original file line numberDiff line numberDiff line change
@@ -2,80 +2,111 @@
22
interface MsgConstructor<T extends Message> {
33
>MsgConstructor : Symbol(MsgConstructor, Decl(collectionPatternNoError.ts, 0, 0))
44
>T : Symbol(T, Decl(collectionPatternNoError.ts, 0, 25))
5-
>Message : Symbol(Message, Decl(collectionPatternNoError.ts, 2, 3))
5+
>Message : Symbol(Message, Decl(collectionPatternNoError.ts, 2, 1))
66

7-
new(data: Array<{}>): T;
8-
>data : Symbol(data, Decl(collectionPatternNoError.ts, 1, 8))
7+
new(data: Array<{}>): T;
8+
>data : Symbol(data, Decl(collectionPatternNoError.ts, 1, 6))
99
>Array : Symbol(Array, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
1010
>T : Symbol(T, Decl(collectionPatternNoError.ts, 0, 25))
11-
}
12-
class Message {
13-
>Message : Symbol(Message, Decl(collectionPatternNoError.ts, 2, 3))
11+
}
12+
class Message {
13+
>Message : Symbol(Message, Decl(collectionPatternNoError.ts, 2, 1))
1414

15-
clone(): this {
16-
>clone : Symbol(Message.clone, Decl(collectionPatternNoError.ts, 3, 17))
15+
clone(): this {
16+
>clone : Symbol(Message.clone, Decl(collectionPatternNoError.ts, 3, 15))
1717

18-
return this;
19-
>this : Symbol(Message, Decl(collectionPatternNoError.ts, 2, 3))
20-
}
21-
}
22-
interface MessageList<T extends Message> extends Message {
23-
>MessageList : Symbol(MessageList, Decl(collectionPatternNoError.ts, 7, 3))
24-
>T : Symbol(T, Decl(collectionPatternNoError.ts, 8, 24))
25-
>Message : Symbol(Message, Decl(collectionPatternNoError.ts, 2, 3))
26-
>Message : Symbol(Message, Decl(collectionPatternNoError.ts, 2, 3))
27-
28-
methodOnMessageList(): T[];
29-
>methodOnMessageList : Symbol(MessageList.methodOnMessageList, Decl(collectionPatternNoError.ts, 8, 60))
30-
>T : Symbol(T, Decl(collectionPatternNoError.ts, 8, 24))
18+
return this;
19+
>this : Symbol(Message, Decl(collectionPatternNoError.ts, 2, 1))
3120
}
32-
33-
function fetchMsg<V extends Message>(protoCtor: MsgConstructor<V>): V {
34-
>fetchMsg : Symbol(fetchMsg, Decl(collectionPatternNoError.ts, 10, 3))
35-
>V : Symbol(V, Decl(collectionPatternNoError.ts, 12, 20))
36-
>Message : Symbol(Message, Decl(collectionPatternNoError.ts, 2, 3))
37-
>protoCtor : Symbol(protoCtor, Decl(collectionPatternNoError.ts, 12, 39))
21+
}
22+
interface MessageList<T extends Message> extends Message {
23+
>MessageList : Symbol(MessageList, Decl(collectionPatternNoError.ts, 7, 1))
24+
>T : Symbol(T, Decl(collectionPatternNoError.ts, 8, 22))
25+
>Message : Symbol(Message, Decl(collectionPatternNoError.ts, 2, 1))
26+
>Message : Symbol(Message, Decl(collectionPatternNoError.ts, 2, 1))
27+
28+
methodOnMessageList(): T[];
29+
>methodOnMessageList : Symbol(MessageList.methodOnMessageList, Decl(collectionPatternNoError.ts, 8, 58))
30+
>T : Symbol(T, Decl(collectionPatternNoError.ts, 8, 22))
31+
}
32+
33+
function fetchMsg<V extends Message>(protoCtor: MsgConstructor<V>): V {
34+
>fetchMsg : Symbol(fetchMsg, Decl(collectionPatternNoError.ts, 10, 1))
35+
>V : Symbol(V, Decl(collectionPatternNoError.ts, 12, 18))
36+
>Message : Symbol(Message, Decl(collectionPatternNoError.ts, 2, 1))
37+
>protoCtor : Symbol(protoCtor, Decl(collectionPatternNoError.ts, 12, 37))
3838
>MsgConstructor : Symbol(MsgConstructor, Decl(collectionPatternNoError.ts, 0, 0))
39-
>V : Symbol(V, Decl(collectionPatternNoError.ts, 12, 20))
40-
>V : Symbol(V, Decl(collectionPatternNoError.ts, 12, 20))
39+
>V : Symbol(V, Decl(collectionPatternNoError.ts, 12, 18))
40+
>V : Symbol(V, Decl(collectionPatternNoError.ts, 12, 18))
4141

42-
return null!;
43-
}
44-
45-
class DataProvider<T extends Message, U extends MessageList<T>> {
46-
>DataProvider : Symbol(DataProvider, Decl(collectionPatternNoError.ts, 14, 3))
47-
>T : Symbol(T, Decl(collectionPatternNoError.ts, 16, 21))
48-
>Message : Symbol(Message, Decl(collectionPatternNoError.ts, 2, 3))
49-
>U : Symbol(U, Decl(collectionPatternNoError.ts, 16, 39))
50-
>MessageList : Symbol(MessageList, Decl(collectionPatternNoError.ts, 7, 3))
51-
>T : Symbol(T, Decl(collectionPatternNoError.ts, 16, 21))
52-
53-
constructor(
54-
private readonly message: MsgConstructor<T>,
55-
>message : Symbol(DataProvider.message, Decl(collectionPatternNoError.ts, 17, 16))
42+
return null!;
43+
}
44+
45+
class DataProvider<T extends Message, U extends MessageList<T>> {
46+
>DataProvider : Symbol(DataProvider, Decl(collectionPatternNoError.ts, 14, 1))
47+
>T : Symbol(T, Decl(collectionPatternNoError.ts, 16, 19))
48+
>Message : Symbol(Message, Decl(collectionPatternNoError.ts, 2, 1))
49+
>U : Symbol(U, Decl(collectionPatternNoError.ts, 16, 37))
50+
>MessageList : Symbol(MessageList, Decl(collectionPatternNoError.ts, 7, 1))
51+
>T : Symbol(T, Decl(collectionPatternNoError.ts, 16, 19))
52+
53+
constructor(
54+
private readonly message: MsgConstructor<T>,
55+
>message : Symbol(DataProvider.message, Decl(collectionPatternNoError.ts, 17, 14))
5656
>MsgConstructor : Symbol(MsgConstructor, Decl(collectionPatternNoError.ts, 0, 0))
57-
>T : Symbol(T, Decl(collectionPatternNoError.ts, 16, 21))
57+
>T : Symbol(T, Decl(collectionPatternNoError.ts, 16, 19))
5858

59-
private readonly messageList: MsgConstructor<U>,
60-
>messageList : Symbol(DataProvider.messageList, Decl(collectionPatternNoError.ts, 18, 52))
59+
private readonly messageList: MsgConstructor<U>,
60+
>messageList : Symbol(DataProvider.messageList, Decl(collectionPatternNoError.ts, 18, 48))
6161
>MsgConstructor : Symbol(MsgConstructor, Decl(collectionPatternNoError.ts, 0, 0))
62-
>U : Symbol(U, Decl(collectionPatternNoError.ts, 16, 39))
63-
64-
) {}
65-
66-
fetch() {
67-
>fetch : Symbol(DataProvider.fetch, Decl(collectionPatternNoError.ts, 20, 8))
68-
69-
const messageList = fetchMsg(this.messageList);
70-
>messageList : Symbol(messageList, Decl(collectionPatternNoError.ts, 23, 11))
71-
>fetchMsg : Symbol(fetchMsg, Decl(collectionPatternNoError.ts, 10, 3))
72-
>this.messageList : Symbol(DataProvider.messageList, Decl(collectionPatternNoError.ts, 18, 52))
73-
>this : Symbol(DataProvider, Decl(collectionPatternNoError.ts, 14, 3))
74-
>messageList : Symbol(DataProvider.messageList, Decl(collectionPatternNoError.ts, 18, 52))
75-
76-
messageList.methodOnMessageList();
77-
>messageList.methodOnMessageList : Symbol(MessageList.methodOnMessageList, Decl(collectionPatternNoError.ts, 8, 60))
78-
>messageList : Symbol(messageList, Decl(collectionPatternNoError.ts, 23, 11))
79-
>methodOnMessageList : Symbol(MessageList.methodOnMessageList, Decl(collectionPatternNoError.ts, 8, 60))
80-
}
62+
>U : Symbol(U, Decl(collectionPatternNoError.ts, 16, 37))
63+
64+
) { }
65+
66+
fetch() {
67+
>fetch : Symbol(DataProvider.fetch, Decl(collectionPatternNoError.ts, 20, 7))
68+
69+
const messageList = fetchMsg(this.messageList);
70+
>messageList : Symbol(messageList, Decl(collectionPatternNoError.ts, 23, 9))
71+
>fetchMsg : Symbol(fetchMsg, Decl(collectionPatternNoError.ts, 10, 1))
72+
>this.messageList : Symbol(DataProvider.messageList, Decl(collectionPatternNoError.ts, 18, 48))
73+
>this : Symbol(DataProvider, Decl(collectionPatternNoError.ts, 14, 1))
74+
>messageList : Symbol(DataProvider.messageList, Decl(collectionPatternNoError.ts, 18, 48))
75+
76+
messageList.methodOnMessageList();
77+
>messageList.methodOnMessageList : Symbol(MessageList.methodOnMessageList, Decl(collectionPatternNoError.ts, 8, 58))
78+
>messageList : Symbol(messageList, Decl(collectionPatternNoError.ts, 23, 9))
79+
>methodOnMessageList : Symbol(MessageList.methodOnMessageList, Decl(collectionPatternNoError.ts, 8, 58))
8180
}
81+
}
82+
83+
// The same bug as the above but using indexed accesses
84+
// (won't surface directly unless unsound indexed access assignments are forbidden)
85+
function f<
86+
>f : Symbol(f, Decl(collectionPatternNoError.ts, 26, 1))
87+
88+
U extends {TType: MessageList<T>},
89+
>U : Symbol(U, Decl(collectionPatternNoError.ts, 30, 11))
90+
>TType : Symbol(TType, Decl(collectionPatternNoError.ts, 31, 13))
91+
>MessageList : Symbol(MessageList, Decl(collectionPatternNoError.ts, 7, 1))
92+
>T : Symbol(T, Decl(collectionPatternNoError.ts, 31, 36))
93+
94+
T extends Message
95+
>T : Symbol(T, Decl(collectionPatternNoError.ts, 31, 36))
96+
>Message : Symbol(Message, Decl(collectionPatternNoError.ts, 2, 1))
97+
98+
>(message: MsgConstructor<T>, messageList: MsgConstructor<U["TType"]>) {
99+
>message : Symbol(message, Decl(collectionPatternNoError.ts, 33, 2))
100+
>MsgConstructor : Symbol(MsgConstructor, Decl(collectionPatternNoError.ts, 0, 0))
101+
>T : Symbol(T, Decl(collectionPatternNoError.ts, 31, 36))
102+
>messageList : Symbol(messageList, Decl(collectionPatternNoError.ts, 33, 29))
103+
>MsgConstructor : Symbol(MsgConstructor, Decl(collectionPatternNoError.ts, 0, 0))
104+
>U : Symbol(U, Decl(collectionPatternNoError.ts, 30, 11))
105+
106+
fetchMsg(messageList).methodOnMessageList();
107+
>fetchMsg(messageList).methodOnMessageList : Symbol(MessageList.methodOnMessageList, Decl(collectionPatternNoError.ts, 8, 58))
108+
>fetchMsg : Symbol(fetchMsg, Decl(collectionPatternNoError.ts, 10, 1))
109+
>messageList : Symbol(messageList, Decl(collectionPatternNoError.ts, 33, 29))
110+
>methodOnMessageList : Symbol(MessageList.methodOnMessageList, Decl(collectionPatternNoError.ts, 8, 58))
111+
}
112+

0 commit comments

Comments
 (0)