diff --git a/tests/cases/conformance/controlFlow/controlFlowGenericTypes.ts b/tests/cases/conformance/controlFlow/controlFlowGenericTypes.ts index 50c2c4fdf14..d4dcd885a45 100644 --- a/tests/cases/conformance/controlFlow/controlFlowGenericTypes.ts +++ b/tests/cases/conformance/controlFlow/controlFlowGenericTypes.ts @@ -140,23 +140,50 @@ function once>(emittingObject: T, eventName: keyo emittingObject.off(eventName as typeof eventName, 0); } -// In an element access obj[x], we consider obj to be in a constraint position, except when obj is of -// a generic type without a nullable constraint and x is a generic type. This is because when both obj -// and x are of generic types T and K, we want the resulting type to be T[K]. +// In an element access obj[key], we consider obj to be in a constraint position, except when +// obj and key both have generic types. When obj and key are of generic types T and K, we want +// the resulting type to be T[K]. function fx1(obj: T, key: K) { const x1 = obj[key]; const x2 = obj && obj[key]; + const x3 = obj?.[key]; } function fx2, K extends keyof T>(obj: T, key: K) { const x1 = obj[key]; const x2 = obj && obj[key]; + const x3 = obj?.[key]; } function fx3 | undefined, K extends keyof T>(obj: T, key: K) { + const x1 = obj[key]; + const x2 = obj && obj[key]; + const x3 = obj?.[key]; +} + +function fx4(obj: T, key: K) { + const x1 = obj[key]; + const x2 = obj && obj[key]; + const x3 = obj?.[key]; +} + +function fx5(obj: T, key: K) { + const x1 = obj[key]; + const x2 = obj && obj[key]; + const x3 = obj?.[key]; +} + +function fx6(obj: T | null | undefined, key: K) { const x1 = obj[key]; // Error const x2 = obj && obj[key]; + const x3 = obj?.[key]; +} + +function fx7(obj: { x: T } | null | undefined, key: K) { + const x1 = obj.x[key]; // Error + const x2 = obj && obj.x[key]; + const x3 = obj?.x[key]; } // Repro from #44166 @@ -166,8 +193,8 @@ class TableBaseEnum< InternalSpec extends Record | undefined = undefined> { m() { let iSpec = null! as InternalSpec; - iSpec[null! as keyof InternalSpec]; // Error, object possibly undefined - iSpec[null! as keyof PublicSpec]; // Error, object possibly undefined + iSpec[null! as keyof InternalSpec]; + iSpec[null! as keyof PublicSpec]; // Error if (iSpec === undefined) { return; }