Skip to content

Commit b93a10b

Browse files
committed
Add tests
1 parent 7cba33c commit b93a10b

File tree

1 file changed

+32
-5
lines changed

1 file changed

+32
-5
lines changed

tests/cases/conformance/controlFlow/controlFlowGenericTypes.ts

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,23 +140,50 @@ function once<ET, T extends EventEmitter<ET>>(emittingObject: T, eventName: keyo
140140
emittingObject.off(eventName as typeof eventName, 0);
141141
}
142142

143-
// In an element access obj[x], we consider obj to be in a constraint position, except when obj is of
144-
// a generic type without a nullable constraint and x is a generic type. This is because when both obj
145-
// and x are of generic types T and K, we want the resulting type to be T[K].
143+
// In an element access obj[key], we consider obj to be in a constraint position, except when
144+
// obj and key both have generic types. When obj and key are of generic types T and K, we want
145+
// the resulting type to be T[K].
146146

147147
function fx1<T, K extends keyof T>(obj: T, key: K) {
148148
const x1 = obj[key];
149149
const x2 = obj && obj[key];
150+
const x3 = obj?.[key];
150151
}
151152

152153
function fx2<T extends Record<keyof T, string>, K extends keyof T>(obj: T, key: K) {
153154
const x1 = obj[key];
154155
const x2 = obj && obj[key];
156+
const x3 = obj?.[key];
155157
}
156158

157159
function fx3<T extends Record<keyof T, string> | undefined, K extends keyof T>(obj: T, key: K) {
160+
const x1 = obj[key];
161+
const x2 = obj && obj[key];
162+
const x3 = obj?.[key];
163+
}
164+
165+
function fx4<T extends unknown, K extends keyof T>(obj: T, key: K) {
166+
const x1 = obj[key];
167+
const x2 = obj && obj[key];
168+
const x3 = obj?.[key];
169+
}
170+
171+
function fx5<T extends {} | null | undefined, K extends keyof T>(obj: T, key: K) {
172+
const x1 = obj[key];
173+
const x2 = obj && obj[key];
174+
const x3 = obj?.[key];
175+
}
176+
177+
function fx6<T, K extends keyof T>(obj: T | null | undefined, key: K) {
158178
const x1 = obj[key]; // Error
159179
const x2 = obj && obj[key];
180+
const x3 = obj?.[key];
181+
}
182+
183+
function fx7<T, K extends keyof T>(obj: { x: T } | null | undefined, key: K) {
184+
const x1 = obj.x[key]; // Error
185+
const x2 = obj && obj.x[key];
186+
const x3 = obj?.x[key];
160187
}
161188

162189
// Repro from #44166
@@ -166,8 +193,8 @@ class TableBaseEnum<
166193
InternalSpec extends Record<keyof PublicSpec, any> | undefined = undefined> {
167194
m() {
168195
let iSpec = null! as InternalSpec;
169-
iSpec[null! as keyof InternalSpec]; // Error, object possibly undefined
170-
iSpec[null! as keyof PublicSpec]; // Error, object possibly undefined
196+
iSpec[null! as keyof InternalSpec];
197+
iSpec[null! as keyof PublicSpec]; // Error
171198
if (iSpec === undefined) {
172199
return;
173200
}

0 commit comments

Comments
 (0)