@@ -405,3 +405,123 @@ function once<ET, T extends EventEmitter<ET>>(emittingObject: T, eventName: keyo
405405>eventName : Symbol(eventName, Decl(controlFlowGenericTypes.ts, 135, 64))
406406}
407407
408+ // In an element access obj[x], we consider obj to be in a constraint position, except when obj is of
409+ // a generic type without a nullable constraint and x is a generic type. This is because when both obj
410+ // and x are of generic types T and K, we want the resulting type to be T[K].
411+
412+ function fx1<T, K extends keyof T>(obj: T, key: K) {
413+ >fx1 : Symbol(fx1, Decl(controlFlowGenericTypes.ts, 138, 1))
414+ >T : Symbol(T, Decl(controlFlowGenericTypes.ts, 144, 13))
415+ >K : Symbol(K, Decl(controlFlowGenericTypes.ts, 144, 15))
416+ >T : Symbol(T, Decl(controlFlowGenericTypes.ts, 144, 13))
417+ >obj : Symbol(obj, Decl(controlFlowGenericTypes.ts, 144, 35))
418+ >T : Symbol(T, Decl(controlFlowGenericTypes.ts, 144, 13))
419+ >key : Symbol(key, Decl(controlFlowGenericTypes.ts, 144, 42))
420+ >K : Symbol(K, Decl(controlFlowGenericTypes.ts, 144, 15))
421+
422+ const x1 = obj[key];
423+ >x1 : Symbol(x1, Decl(controlFlowGenericTypes.ts, 145, 9))
424+ >obj : Symbol(obj, Decl(controlFlowGenericTypes.ts, 144, 35))
425+ >key : Symbol(key, Decl(controlFlowGenericTypes.ts, 144, 42))
426+
427+ const x2 = obj && obj[key];
428+ >x2 : Symbol(x2, Decl(controlFlowGenericTypes.ts, 146, 9))
429+ >obj : Symbol(obj, Decl(controlFlowGenericTypes.ts, 144, 35))
430+ >obj : Symbol(obj, Decl(controlFlowGenericTypes.ts, 144, 35))
431+ >key : Symbol(key, Decl(controlFlowGenericTypes.ts, 144, 42))
432+ }
433+
434+ function fx2<T extends Record<keyof T, string>, K extends keyof T>(obj: T, key: K) {
435+ >fx2 : Symbol(fx2, Decl(controlFlowGenericTypes.ts, 147, 1))
436+ >T : Symbol(T, Decl(controlFlowGenericTypes.ts, 149, 13))
437+ >Record : Symbol(Record, Decl(lib.es5.d.ts, --, --))
438+ >T : Symbol(T, Decl(controlFlowGenericTypes.ts, 149, 13))
439+ >K : Symbol(K, Decl(controlFlowGenericTypes.ts, 149, 47))
440+ >T : Symbol(T, Decl(controlFlowGenericTypes.ts, 149, 13))
441+ >obj : Symbol(obj, Decl(controlFlowGenericTypes.ts, 149, 67))
442+ >T : Symbol(T, Decl(controlFlowGenericTypes.ts, 149, 13))
443+ >key : Symbol(key, Decl(controlFlowGenericTypes.ts, 149, 74))
444+ >K : Symbol(K, Decl(controlFlowGenericTypes.ts, 149, 47))
445+
446+ const x1 = obj[key];
447+ >x1 : Symbol(x1, Decl(controlFlowGenericTypes.ts, 150, 9))
448+ >obj : Symbol(obj, Decl(controlFlowGenericTypes.ts, 149, 67))
449+ >key : Symbol(key, Decl(controlFlowGenericTypes.ts, 149, 74))
450+
451+ const x2 = obj && obj[key];
452+ >x2 : Symbol(x2, Decl(controlFlowGenericTypes.ts, 151, 9))
453+ >obj : Symbol(obj, Decl(controlFlowGenericTypes.ts, 149, 67))
454+ >obj : Symbol(obj, Decl(controlFlowGenericTypes.ts, 149, 67))
455+ >key : Symbol(key, Decl(controlFlowGenericTypes.ts, 149, 74))
456+ }
457+
458+ function fx3<T extends Record<keyof T, string> | undefined, K extends keyof T>(obj: T, key: K) {
459+ >fx3 : Symbol(fx3, Decl(controlFlowGenericTypes.ts, 152, 1))
460+ >T : Symbol(T, Decl(controlFlowGenericTypes.ts, 154, 13))
461+ >Record : Symbol(Record, Decl(lib.es5.d.ts, --, --))
462+ >T : Symbol(T, Decl(controlFlowGenericTypes.ts, 154, 13))
463+ >K : Symbol(K, Decl(controlFlowGenericTypes.ts, 154, 59))
464+ >T : Symbol(T, Decl(controlFlowGenericTypes.ts, 154, 13))
465+ >obj : Symbol(obj, Decl(controlFlowGenericTypes.ts, 154, 79))
466+ >T : Symbol(T, Decl(controlFlowGenericTypes.ts, 154, 13))
467+ >key : Symbol(key, Decl(controlFlowGenericTypes.ts, 154, 86))
468+ >K : Symbol(K, Decl(controlFlowGenericTypes.ts, 154, 59))
469+
470+ const x1 = obj[key]; // Error
471+ >x1 : Symbol(x1, Decl(controlFlowGenericTypes.ts, 155, 9))
472+ >obj : Symbol(obj, Decl(controlFlowGenericTypes.ts, 154, 79))
473+ >key : Symbol(key, Decl(controlFlowGenericTypes.ts, 154, 86))
474+
475+ const x2 = obj && obj[key];
476+ >x2 : Symbol(x2, Decl(controlFlowGenericTypes.ts, 156, 9))
477+ >obj : Symbol(obj, Decl(controlFlowGenericTypes.ts, 154, 79))
478+ >obj : Symbol(obj, Decl(controlFlowGenericTypes.ts, 154, 79))
479+ >key : Symbol(key, Decl(controlFlowGenericTypes.ts, 154, 86))
480+ }
481+
482+ // Repro from #44166
483+
484+ class TableBaseEnum<
485+ >TableBaseEnum : Symbol(TableBaseEnum, Decl(controlFlowGenericTypes.ts, 157, 1))
486+
487+ PublicSpec extends Record<keyof InternalSpec, any>,
488+ >PublicSpec : Symbol(PublicSpec, Decl(controlFlowGenericTypes.ts, 161, 20))
489+ >Record : Symbol(Record, Decl(lib.es5.d.ts, --, --))
490+ >InternalSpec : Symbol(InternalSpec, Decl(controlFlowGenericTypes.ts, 162, 55))
491+
492+ InternalSpec extends Record<keyof PublicSpec, any> | undefined = undefined> {
493+ >InternalSpec : Symbol(InternalSpec, Decl(controlFlowGenericTypes.ts, 162, 55))
494+ >Record : Symbol(Record, Decl(lib.es5.d.ts, --, --))
495+ >PublicSpec : Symbol(PublicSpec, Decl(controlFlowGenericTypes.ts, 161, 20))
496+
497+ m() {
498+ >m : Symbol(TableBaseEnum.m, Decl(controlFlowGenericTypes.ts, 163, 82))
499+
500+ let iSpec = null! as InternalSpec;
501+ >iSpec : Symbol(iSpec, Decl(controlFlowGenericTypes.ts, 165, 11))
502+ >InternalSpec : Symbol(InternalSpec, Decl(controlFlowGenericTypes.ts, 162, 55))
503+
504+ iSpec[null! as keyof InternalSpec]; // Error, object possibly undefined
505+ >iSpec : Symbol(iSpec, Decl(controlFlowGenericTypes.ts, 165, 11))
506+ >InternalSpec : Symbol(InternalSpec, Decl(controlFlowGenericTypes.ts, 162, 55))
507+
508+ iSpec[null! as keyof PublicSpec]; // Error, object possibly undefined
509+ >iSpec : Symbol(iSpec, Decl(controlFlowGenericTypes.ts, 165, 11))
510+ >PublicSpec : Symbol(PublicSpec, Decl(controlFlowGenericTypes.ts, 161, 20))
511+
512+ if (iSpec === undefined) {
513+ >iSpec : Symbol(iSpec, Decl(controlFlowGenericTypes.ts, 165, 11))
514+ >undefined : Symbol(undefined)
515+
516+ return;
517+ }
518+ iSpec[null! as keyof InternalSpec];
519+ >iSpec : Symbol(iSpec, Decl(controlFlowGenericTypes.ts, 165, 11))
520+ >InternalSpec : Symbol(InternalSpec, Decl(controlFlowGenericTypes.ts, 162, 55))
521+
522+ iSpec[null! as keyof PublicSpec];
523+ >iSpec : Symbol(iSpec, Decl(controlFlowGenericTypes.ts, 165, 11))
524+ >PublicSpec : Symbol(PublicSpec, Decl(controlFlowGenericTypes.ts, 161, 20))
525+ }
526+ }
527+
0 commit comments