Correct assignability for keyof types and type parameters

This commit is contained in:
Anders Hejlsberg 2016-11-03 10:01:27 -07:00
parent ab75ea75d3
commit 83abd048b5
2 changed files with 22 additions and 1 deletions

View File

@ -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(<TypeParameter>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((<IndexType>target).type);
if (constraint) {
if (result = isRelatedTo(source, getIndexType(constraint), reportErrors)) {
return result;
}
}
}
if (source.flags & TypeFlags.TypeParameter) {
let constraint = getConstraintOfTypeParameter(<TypeParameter>source);

View File

@ -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