Skip to content

Commit 83abd04

Browse files
committed
Correct assignability for keyof types and type parameters
1 parent ab75ea7 commit 83abd04

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/compiler/checker.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6825,6 +6825,27 @@ namespace ts {
68256825
}
68266826
}
68276827

6828+
if (target.flags & TypeFlags.TypeParameter) {
6829+
// Given a type parameter K with a constraint keyof T, a type S is
6830+
// assignable to K if S is assignable to keyof T.
6831+
let constraint = getConstraintOfTypeParameter(<TypeParameter>target);
6832+
if (constraint && constraint.flags & TypeFlags.Index) {
6833+
if (result = isRelatedTo(source, constraint, reportErrors)) {
6834+
return result;
6835+
}
6836+
}
6837+
}
6838+
else if (target.flags & TypeFlags.Index) {
6839+
// Given a type parameter T with a constraint C, a type S is assignable to
6840+
// keyof T if S is assignable to keyof C.
6841+
let constraint = getConstraintOfTypeParameter((<IndexType>target).type);
6842+
if (constraint) {
6843+
if (result = isRelatedTo(source, getIndexType(constraint), reportErrors)) {
6844+
return result;
6845+
}
6846+
}
6847+
}
6848+
68286849
if (source.flags & TypeFlags.TypeParameter) {
68296850
let constraint = getConstraintOfTypeParameter(<TypeParameter>source);
68306851

src/compiler/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2709,7 +2709,7 @@ namespace ts {
27092709
EnumLike = Enum | EnumLiteral,
27102710
UnionOrIntersection = Union | Intersection,
27112711
StructuredType = Object | Union | Intersection,
2712-
StructuredOrTypeParameter = StructuredType | TypeParameter,
2712+
StructuredOrTypeParameter = StructuredType | TypeParameter | Index,
27132713

27142714
// 'Narrowable' types are types where narrowing actually narrows.
27152715
// This *should* be every type other than null, undefined, void, and never

0 commit comments

Comments
 (0)