Skip to content

Commit 6d170b4

Browse files
authoredAug 29, 2022
Handle intersections in isGenericTypeWithoutNullableConstraint (#50497)
* Handle intersections in isGenericTypeWithoutNullableConstraint * Add regression test
1 parent ed6889c commit 6d170b4

File tree

6 files changed

+76
-2
lines changed

6 files changed

+76
-2
lines changed
 

‎src/compiler/checker.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -25789,8 +25789,10 @@ namespace ts {
2578925789
!!(type.flags & TypeFlags.Instantiable && getBaseConstraintOrType(type).flags & (TypeFlags.Nullable | TypeFlags.Union));
2579025790
}
2579125791

25792-
function isGenericTypeWithoutNullableConstraint(type: Type) {
25793-
return !!(type.flags & TypeFlags.Instantiable && !maybeTypeOfKind(getBaseConstraintOrType(type), TypeFlags.Nullable));
25792+
function isGenericTypeWithoutNullableConstraint(type: Type): boolean {
25793+
return type.flags & TypeFlags.Intersection ?
25794+
some((type as IntersectionType).types, isGenericTypeWithoutNullableConstraint) :
25795+
!!(type.flags & TypeFlags.Instantiable && !maybeTypeOfKind(getBaseConstraintOrType(type), TypeFlags.Nullable));
2579425796
}
2579525797

2579625798
function hasContextualTypeWithNoGenericTypes(node: Node, checkMode: CheckMode | undefined) {

‎tests/baselines/reference/controlFlowGenericTypes.errors.txt

+8
Original file line numberDiff line numberDiff line change
@@ -240,4 +240,12 @@ tests/cases/conformance/controlFlow/controlFlowGenericTypes.ts(168,9): error TS2
240240
control[key] = value;
241241
}
242242
}
243+
244+
// Repro from #50465
245+
246+
type Column<T> = (keyof T extends never ? { id?: number | string } : { id: T }) & { title?: string; }
247+
248+
function getColumnProperty<T>(column: Column<T>, key: keyof Column<T>) {
249+
return column[key];
250+
}
243251

‎tests/baselines/reference/controlFlowGenericTypes.js

+11
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,14 @@ function update<T extends Control, K extends keyof T>(control : T | undefined, k
210210
control[key] = value;
211211
}
212212
}
213+
214+
// Repro from #50465
215+
216+
type Column<T> = (keyof T extends never ? { id?: number | string } : { id: T }) & { title?: string; }
217+
218+
function getColumnProperty<T>(column: Column<T>, key: keyof Column<T>) {
219+
return column[key];
220+
}
213221

214222

215223
//// [controlFlowGenericTypes.js]
@@ -368,3 +376,6 @@ function update(control, key, value) {
368376
control[key] = value;
369377
}
370378
}
379+
function getColumnProperty(column, key) {
380+
return column[key];
381+
}

‎tests/baselines/reference/controlFlowGenericTypes.symbols

+26
Original file line numberDiff line numberDiff line change
@@ -626,3 +626,29 @@ function update<T extends Control, K extends keyof T>(control : T | undefined, k
626626
}
627627
}
628628

629+
// Repro from #50465
630+
631+
type Column<T> = (keyof T extends never ? { id?: number | string } : { id: T }) & { title?: string; }
632+
>Column : Symbol(Column, Decl(controlFlowGenericTypes.ts, 210, 1))
633+
>T : Symbol(T, Decl(controlFlowGenericTypes.ts, 214, 12))
634+
>T : Symbol(T, Decl(controlFlowGenericTypes.ts, 214, 12))
635+
>id : Symbol(id, Decl(controlFlowGenericTypes.ts, 214, 43))
636+
>id : Symbol(id, Decl(controlFlowGenericTypes.ts, 214, 70))
637+
>T : Symbol(T, Decl(controlFlowGenericTypes.ts, 214, 12))
638+
>title : Symbol(title, Decl(controlFlowGenericTypes.ts, 214, 83))
639+
640+
function getColumnProperty<T>(column: Column<T>, key: keyof Column<T>) {
641+
>getColumnProperty : Symbol(getColumnProperty, Decl(controlFlowGenericTypes.ts, 214, 101))
642+
>T : Symbol(T, Decl(controlFlowGenericTypes.ts, 216, 27))
643+
>column : Symbol(column, Decl(controlFlowGenericTypes.ts, 216, 30))
644+
>Column : Symbol(Column, Decl(controlFlowGenericTypes.ts, 210, 1))
645+
>T : Symbol(T, Decl(controlFlowGenericTypes.ts, 216, 27))
646+
>key : Symbol(key, Decl(controlFlowGenericTypes.ts, 216, 48))
647+
>Column : Symbol(Column, Decl(controlFlowGenericTypes.ts, 210, 1))
648+
>T : Symbol(T, Decl(controlFlowGenericTypes.ts, 216, 27))
649+
650+
return column[key];
651+
>column : Symbol(column, Decl(controlFlowGenericTypes.ts, 216, 30))
652+
>key : Symbol(key, Decl(controlFlowGenericTypes.ts, 216, 48))
653+
}
654+

‎tests/baselines/reference/controlFlowGenericTypes.types

+19
Original file line numberDiff line numberDiff line change
@@ -583,3 +583,22 @@ function update<T extends Control, K extends keyof T>(control : T | undefined, k
583583
}
584584
}
585585

586+
// Repro from #50465
587+
588+
type Column<T> = (keyof T extends never ? { id?: number | string } : { id: T }) & { title?: string; }
589+
>Column : Column<T>
590+
>id : string | number | undefined
591+
>id : T
592+
>title : string | undefined
593+
594+
function getColumnProperty<T>(column: Column<T>, key: keyof Column<T>) {
595+
>getColumnProperty : <T>(column: Column<T>, key: keyof Column<T>) => Column<T>["title" | keyof (keyof T extends never ? { id?: string | number | undefined; } : { id: T; })]
596+
>column : Column<T>
597+
>key : "title" | keyof (keyof T extends never ? { id?: string | number | undefined; } : { id: T; })
598+
599+
return column[key];
600+
>column[key] : Column<T>["title" | keyof (keyof T extends never ? { id?: string | number | undefined; } : { id: T; })]
601+
>column : Column<T>
602+
>key : "title" | keyof (keyof T extends never ? { id?: string | number | undefined; } : { id: T; })
603+
}
604+

‎tests/cases/conformance/controlFlow/controlFlowGenericTypes.ts

+8
Original file line numberDiff line numberDiff line change
@@ -211,3 +211,11 @@ function update<T extends Control, K extends keyof T>(control : T | undefined, k
211211
control[key] = value;
212212
}
213213
}
214+
215+
// Repro from #50465
216+
217+
type Column<T> = (keyof T extends never ? { id?: number | string } : { id: T }) & { title?: string; }
218+
219+
function getColumnProperty<T>(column: Column<T>, key: keyof Column<T>) {
220+
return column[key];
221+
}

0 commit comments

Comments
 (0)
Please sign in to comment.