Skip to content

Commit 9ed5ad1

Browse files
committed
Unconstrained type parameter not assignable to non-primitive object
1 parent 04da707 commit 9ed5ad1

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

src/compiler/checker.ts

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7172,8 +7172,7 @@ namespace ts {
71727172
if (source.flags & TypeFlags.Enum && target.flags & TypeFlags.Enum && isEnumTypeRelatedTo(<EnumType>source, <EnumType>target, errorReporter)) return true;
71737173
if (source.flags & TypeFlags.Undefined && (!strictNullChecks || target.flags & (TypeFlags.Undefined | TypeFlags.Void))) return true;
71747174
if (source.flags & TypeFlags.Null && (!strictNullChecks || target.flags & TypeFlags.Null)) return true;
7175-
if (source.flags & TypeFlags.Object && target === nonPrimitiveType) return true;
7176-
if (source.flags & TypeFlags.Primitive && target === nonPrimitiveType) return false;
7175+
if (source.flags & TypeFlags.Object && target.flags & TypeFlags.NonPrimitive) return true;
71777176
if (relation === assignableRelation || relation === comparableRelation) {
71787177
if (source.flags & TypeFlags.Any) return true;
71797178
if ((source.flags & TypeFlags.Number | source.flags & TypeFlags.NumberLiteral) && target.flags & TypeFlags.EnumLike) return true;
@@ -7457,19 +7456,19 @@ namespace ts {
74577456
}
74587457
else {
74597458
let constraint = getConstraintOfTypeParameter(<TypeParameter>source);
7460-
7461-
if (!constraint || constraint.flags & TypeFlags.Any) {
7462-
constraint = emptyObjectType;
7463-
}
7464-
7465-
// The constraint may need to be further instantiated with its 'this' type.
7466-
constraint = getTypeWithThisArgument(constraint, source);
7467-
7468-
// Report constraint errors only if the constraint is not the empty object type
7469-
const reportConstraintErrors = reportErrors && constraint !== emptyObjectType;
7470-
if (result = isRelatedTo(constraint, target, reportConstraintErrors)) {
7471-
errorInfo = saveErrorInfo;
7472-
return result;
7459+
// A type parameter with no constraint is not related to the non-primitive object type.
7460+
if (constraint || !(target.flags & TypeFlags.NonPrimitive)) {
7461+
if (!constraint || constraint.flags & TypeFlags.Any) {
7462+
constraint = emptyObjectType;
7463+
}
7464+
// The constraint may need to be further instantiated with its 'this' type.
7465+
constraint = getTypeWithThisArgument(constraint, source);
7466+
// Report constraint errors only if the constraint is not the empty object type
7467+
const reportConstraintErrors = reportErrors && constraint !== emptyObjectType;
7468+
if (result = isRelatedTo(constraint, target, reportConstraintErrors)) {
7469+
errorInfo = saveErrorInfo;
7470+
return result;
7471+
}
74737472
}
74747473
}
74757474
}
@@ -9237,9 +9236,6 @@ namespace ts {
92379236
}
92389237

92399238
function getTypeFacts(type: Type): TypeFacts {
9240-
if (type === nonPrimitiveType) {
9241-
return strictNullChecks ? TypeFacts.ObjectStrictFacts : TypeFacts.ObjectFacts;
9242-
}
92439239
const flags = type.flags;
92449240
if (flags & TypeFlags.String) {
92459241
return strictNullChecks ? TypeFacts.StringStrictFacts : TypeFacts.StringFacts;
@@ -9280,6 +9276,9 @@ namespace ts {
92809276
if (flags & TypeFlags.ESSymbol) {
92819277
return strictNullChecks ? TypeFacts.SymbolStrictFacts : TypeFacts.SymbolFacts;
92829278
}
9279+
if (flags & TypeFlags.NonPrimitive) {
9280+
return strictNullChecks ? TypeFacts.ObjectStrictFacts : TypeFacts.ObjectFacts;
9281+
}
92839282
if (flags & TypeFlags.TypeParameter) {
92849283
const constraint = getConstraintOfTypeParameter(<TypeParameter>type);
92859284
return getTypeFacts(constraint || emptyObjectType);

0 commit comments

Comments
 (0)