mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-23 07:07:09 -05:00
Check constraints deeply on singleton types.
This commit is contained in:
@@ -15764,23 +15764,13 @@ namespace ts {
|
||||
}
|
||||
}
|
||||
|
||||
if (isLiteralType(source) && !typeCouldHaveNoTopLevelSingletonTypes(target)) {
|
||||
if (isLiteralType(source) && !typeCouldHaveTopLevelSingletonTypes(target)) {
|
||||
generalizedSourceType = getTypeNameForErrorDisplay(getBaseTypeOfLiteralType(source));
|
||||
}
|
||||
|
||||
reportError(message, generalizedSourceType, targetType);
|
||||
}
|
||||
|
||||
function typeCouldHaveNoTopLevelSingletonTypes(type: Type) {
|
||||
return forEachType(type, typeCouldHaveNoTopLevelSingletonTypesWorker);
|
||||
}
|
||||
|
||||
function typeCouldHaveNoTopLevelSingletonTypesWorker(type: Type): boolean {
|
||||
return (type.flags & TypeFlags.Intersection)
|
||||
? !!forEach((type as IntersectionType).types, typeCouldHaveNoTopLevelSingletonTypesWorker)
|
||||
: isUnitType(type) || !!(type.flags & TypeFlags.Instantiable);
|
||||
}
|
||||
|
||||
function tryElaborateErrorsForPrimitivesAndObjects(source: Type, target: Type) {
|
||||
const sourceType = symbolValueDeclarationIsContextSensitive(source.symbol) ? typeToString(source, source.symbol.valueDeclaration) : typeToString(source);
|
||||
const targetType = symbolValueDeclarationIsContextSensitive(target.symbol) ? typeToString(target, target.symbol.valueDeclaration) : typeToString(target);
|
||||
@@ -17375,6 +17365,21 @@ namespace ts {
|
||||
}
|
||||
}
|
||||
|
||||
function typeCouldHaveTopLevelSingletonTypes(type: Type): boolean {
|
||||
if (type.flags & TypeFlags.UnionOrIntersection) {
|
||||
return !!forEach((type as IntersectionType).types, typeCouldHaveTopLevelSingletonTypes);
|
||||
}
|
||||
|
||||
if (type.flags & TypeFlags.Instantiable) {
|
||||
const constraint = getConstraintOfType(type);
|
||||
if (constraint) {
|
||||
return typeCouldHaveTopLevelSingletonTypes(constraint);
|
||||
}
|
||||
}
|
||||
|
||||
return isUnitType(type);
|
||||
}
|
||||
|
||||
function getBestMatchingType(source: Type, target: UnionOrIntersectionType, isRelatedTo = compareTypesAssignable) {
|
||||
return findMatchingDiscriminantType(source, target, isRelatedTo, /*skipPartial*/ true) ||
|
||||
findMatchingTypeReferenceOrTypeAliasReference(source, target) ||
|
||||
|
||||
Reference in New Issue
Block a user