diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 0dc5477eadb57..01fc87bae36a4 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -25789,8 +25789,10 @@ namespace ts { !!(type.flags & TypeFlags.Instantiable && getBaseConstraintOrType(type).flags & (TypeFlags.Nullable | TypeFlags.Union)); } - function isGenericTypeWithoutNullableConstraint(type: Type) { - return !!(type.flags & TypeFlags.Instantiable && !maybeTypeOfKind(getBaseConstraintOrType(type), TypeFlags.Nullable)); + function isGenericTypeWithoutNullableConstraint(type: Type): boolean { + return type.flags & TypeFlags.Intersection ? + some((type as IntersectionType).types, isGenericTypeWithoutNullableConstraint) : + !!(type.flags & TypeFlags.Instantiable && !maybeTypeOfKind(getBaseConstraintOrType(type), TypeFlags.Nullable)); } function hasContextualTypeWithNoGenericTypes(node: Node, checkMode: CheckMode | undefined) { diff --git a/tests/baselines/reference/controlFlowGenericTypes.errors.txt b/tests/baselines/reference/controlFlowGenericTypes.errors.txt index 3130ba416fb75..b2f1de6b88dd6 100644 --- a/tests/baselines/reference/controlFlowGenericTypes.errors.txt +++ b/tests/baselines/reference/controlFlowGenericTypes.errors.txt @@ -240,4 +240,12 @@ tests/cases/conformance/controlFlow/controlFlowGenericTypes.ts(168,9): error TS2 control[key] = value; } } + + // Repro from #50465 + + type Column = (keyof T extends never ? { id?: number | string } : { id: T }) & { title?: string; } + + function getColumnProperty(column: Column, key: keyof Column) { + return column[key]; + } \ No newline at end of file diff --git a/tests/baselines/reference/controlFlowGenericTypes.js b/tests/baselines/reference/controlFlowGenericTypes.js index a59b496ecd753..fbd4391269639 100644 --- a/tests/baselines/reference/controlFlowGenericTypes.js +++ b/tests/baselines/reference/controlFlowGenericTypes.js @@ -210,6 +210,14 @@ function update(control : T | undefined, k control[key] = value; } } + +// Repro from #50465 + +type Column = (keyof T extends never ? { id?: number | string } : { id: T }) & { title?: string; } + +function getColumnProperty(column: Column, key: keyof Column) { + return column[key]; +} //// [controlFlowGenericTypes.js] @@ -368,3 +376,6 @@ function update(control, key, value) { control[key] = value; } } +function getColumnProperty(column, key) { + return column[key]; +} diff --git a/tests/baselines/reference/controlFlowGenericTypes.symbols b/tests/baselines/reference/controlFlowGenericTypes.symbols index 1a1edf72f0afc..71b328efe87aa 100644 --- a/tests/baselines/reference/controlFlowGenericTypes.symbols +++ b/tests/baselines/reference/controlFlowGenericTypes.symbols @@ -626,3 +626,29 @@ function update(control : T | undefined, k } } +// Repro from #50465 + +type Column = (keyof T extends never ? { id?: number | string } : { id: T }) & { title?: string; } +>Column : Symbol(Column, Decl(controlFlowGenericTypes.ts, 210, 1)) +>T : Symbol(T, Decl(controlFlowGenericTypes.ts, 214, 12)) +>T : Symbol(T, Decl(controlFlowGenericTypes.ts, 214, 12)) +>id : Symbol(id, Decl(controlFlowGenericTypes.ts, 214, 43)) +>id : Symbol(id, Decl(controlFlowGenericTypes.ts, 214, 70)) +>T : Symbol(T, Decl(controlFlowGenericTypes.ts, 214, 12)) +>title : Symbol(title, Decl(controlFlowGenericTypes.ts, 214, 83)) + +function getColumnProperty(column: Column, key: keyof Column) { +>getColumnProperty : Symbol(getColumnProperty, Decl(controlFlowGenericTypes.ts, 214, 101)) +>T : Symbol(T, Decl(controlFlowGenericTypes.ts, 216, 27)) +>column : Symbol(column, Decl(controlFlowGenericTypes.ts, 216, 30)) +>Column : Symbol(Column, Decl(controlFlowGenericTypes.ts, 210, 1)) +>T : Symbol(T, Decl(controlFlowGenericTypes.ts, 216, 27)) +>key : Symbol(key, Decl(controlFlowGenericTypes.ts, 216, 48)) +>Column : Symbol(Column, Decl(controlFlowGenericTypes.ts, 210, 1)) +>T : Symbol(T, Decl(controlFlowGenericTypes.ts, 216, 27)) + + return column[key]; +>column : Symbol(column, Decl(controlFlowGenericTypes.ts, 216, 30)) +>key : Symbol(key, Decl(controlFlowGenericTypes.ts, 216, 48)) +} + diff --git a/tests/baselines/reference/controlFlowGenericTypes.types b/tests/baselines/reference/controlFlowGenericTypes.types index e6ebd4226b700..c02307a26e2a7 100644 --- a/tests/baselines/reference/controlFlowGenericTypes.types +++ b/tests/baselines/reference/controlFlowGenericTypes.types @@ -583,3 +583,22 @@ function update(control : T | undefined, k } } +// Repro from #50465 + +type Column = (keyof T extends never ? { id?: number | string } : { id: T }) & { title?: string; } +>Column : Column +>id : string | number | undefined +>id : T +>title : string | undefined + +function getColumnProperty(column: Column, key: keyof Column) { +>getColumnProperty : (column: Column, key: keyof Column) => Column["title" | keyof (keyof T extends never ? { id?: string | number | undefined; } : { id: T; })] +>column : Column +>key : "title" | keyof (keyof T extends never ? { id?: string | number | undefined; } : { id: T; }) + + return column[key]; +>column[key] : Column["title" | keyof (keyof T extends never ? { id?: string | number | undefined; } : { id: T; })] +>column : Column +>key : "title" | keyof (keyof T extends never ? { id?: string | number | undefined; } : { id: T; }) +} + diff --git a/tests/cases/conformance/controlFlow/controlFlowGenericTypes.ts b/tests/cases/conformance/controlFlow/controlFlowGenericTypes.ts index 3e67f3247da3d..50c2c4fdf147e 100644 --- a/tests/cases/conformance/controlFlow/controlFlowGenericTypes.ts +++ b/tests/cases/conformance/controlFlow/controlFlowGenericTypes.ts @@ -211,3 +211,11 @@ function update(control : T | undefined, k control[key] = value; } } + +// Repro from #50465 + +type Column = (keyof T extends never ? { id?: number | string } : { id: T }) & { title?: string; } + +function getColumnProperty(column: Column, key: keyof Column) { + return column[key]; +}