diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 006b87cb542..b47f6264e3e 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -6825,6 +6825,27 @@ namespace ts { } } + if (target.flags & TypeFlags.TypeParameter) { + // Given a type parameter K with a constraint keyof T, a type S is + // assignable to K if S is assignable to keyof T. + let constraint = getConstraintOfTypeParameter(target); + if (constraint && constraint.flags & TypeFlags.Index) { + if (result = isRelatedTo(source, constraint, reportErrors)) { + return result; + } + } + } + else if (target.flags & TypeFlags.Index) { + // Given a type parameter T with a constraint C, a type S is assignable to + // keyof T if S is assignable to keyof C. + let constraint = getConstraintOfTypeParameter((target).type); + if (constraint) { + if (result = isRelatedTo(source, getIndexType(constraint), reportErrors)) { + return result; + } + } + } + if (source.flags & TypeFlags.TypeParameter) { let constraint = getConstraintOfTypeParameter(source); diff --git a/src/compiler/types.ts b/src/compiler/types.ts index e4c92c502a4..b865beca76b 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -2709,7 +2709,7 @@ namespace ts { EnumLike = Enum | EnumLiteral, UnionOrIntersection = Union | Intersection, StructuredType = Object | Union | Intersection, - StructuredOrTypeParameter = StructuredType | TypeParameter, + StructuredOrTypeParameter = StructuredType | TypeParameter | Index, // 'Narrowable' types are types where narrowing actually narrows. // This *should* be every type other than null, undefined, void, and never