@@ -140,23 +140,50 @@ function once<ET, T extends EventEmitter<ET>>(emittingObject: T, eventName: keyo
140
140
emittingObject . off ( eventName as typeof eventName , 0 ) ;
141
141
}
142
142
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].
146
146
147
147
function fx1 < T , K extends keyof T > ( obj : T , key : K ) {
148
148
const x1 = obj [ key ] ;
149
149
const x2 = obj && obj [ key ] ;
150
+ const x3 = obj ?. [ key ] ;
150
151
}
151
152
152
153
function fx2 < T extends Record < keyof T , string > , K extends keyof T > ( obj : T , key : K ) {
153
154
const x1 = obj [ key ] ;
154
155
const x2 = obj && obj [ key ] ;
156
+ const x3 = obj ?. [ key ] ;
155
157
}
156
158
157
159
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 ) {
158
178
const x1 = obj [ key ] ; // Error
159
179
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 ] ;
160
187
}
161
188
162
189
// Repro from #44166
@@ -166,8 +193,8 @@ class TableBaseEnum<
166
193
InternalSpec extends Record < keyof PublicSpec , any > | undefined = undefined > {
167
194
m ( ) {
168
195
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
171
198
if ( iSpec === undefined ) {
172
199
return ;
173
200
}
0 commit comments