Skip to content

Commit b3a2432

Browse files
committed
Make generic mapped type relationship more inclusive
1 parent 7798f69 commit b3a2432

File tree

1 file changed

+18
-20
lines changed

1 file changed

+18
-20
lines changed

src/compiler/checker.ts

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9454,32 +9454,30 @@ namespace ts {
94549454
}
94559455
}
94569456
}
9457+
else if (isGenericMappedType(target) && !isGenericMappedType(source) && getConstraintTypeFromMappedType(<MappedType>target) === getIndexType(source)) {
9458+
// A source type T is related to a target type { [P in keyof T]: X } if T[P] is related to X.
9459+
const indexedAccessType = getIndexedAccessType(source, getTypeParameterFromMappedType(<MappedType>target));
9460+
const templateType = getTemplateTypeFromMappedType(<MappedType>target);
9461+
if (result = isRelatedTo(indexedAccessType, templateType, reportErrors)) {
9462+
errorInfo = saveErrorInfo;
9463+
return result;
9464+
}
9465+
}
94579466

94589467
if (source.flags & TypeFlags.TypeParameter) {
9459-
// A source type T is related to a target type { [P in keyof T]: X } if T[P] is related to X.
9460-
if (getObjectFlags(target) & ObjectFlags.Mapped && getConstraintTypeFromMappedType(<MappedType>target) === getIndexType(source)) {
9461-
const indexedAccessType = getIndexedAccessType(source, getTypeParameterFromMappedType(<MappedType>target));
9462-
const templateType = getTemplateTypeFromMappedType(<MappedType>target);
9463-
if (result = isRelatedTo(indexedAccessType, templateType, reportErrors)) {
9468+
let constraint = getConstraintOfTypeParameter(<TypeParameter>source);
9469+
// A type parameter with no constraint is not related to the non-primitive object type.
9470+
if (constraint || !(target.flags & TypeFlags.NonPrimitive)) {
9471+
if (!constraint || constraint.flags & TypeFlags.Any) {
9472+
constraint = emptyObjectType;
9473+
}
9474+
// Report constraint errors only if the constraint is not the empty object type
9475+
const reportConstraintErrors = reportErrors && constraint !== emptyObjectType;
9476+
if (result = isRelatedTo(constraint, target, reportConstraintErrors)) {
94649477
errorInfo = saveErrorInfo;
94659478
return result;
94669479
}
94679480
}
9468-
else {
9469-
let constraint = getConstraintOfTypeParameter(<TypeParameter>source);
9470-
// A type parameter with no constraint is not related to the non-primitive object type.
9471-
if (constraint || !(target.flags & TypeFlags.NonPrimitive)) {
9472-
if (!constraint || constraint.flags & TypeFlags.Any) {
9473-
constraint = emptyObjectType;
9474-
}
9475-
// Report constraint errors only if the constraint is not the empty object type
9476-
const reportConstraintErrors = reportErrors && constraint !== emptyObjectType;
9477-
if (result = isRelatedTo(constraint, target, reportConstraintErrors)) {
9478-
errorInfo = saveErrorInfo;
9479-
return result;
9480-
}
9481-
}
9482-
}
94839481
}
94849482
else if (source.flags & TypeFlags.IndexedAccess) {
94859483
// A type S[K] is related to a type T if A[K] is related to T, where K is string-like and

0 commit comments

Comments
 (0)