Skip to content

Commit e570775

Browse files
committed
Do not report weak type errors for intersections
TODO: Report weak type errors for intersections *correctly*. That part's not done yet. Also, fix a couple of errors found by this change.
1 parent beba5de commit e570775

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

src/compiler/checker.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2279,7 +2279,7 @@ namespace ts {
22792279
Debug.assert(typeNode !== undefined, "should always get typenode?");
22802280
const options = { removeComments: true };
22812281
const writer = createTextWriter("");
2282-
const printer = createPrinter(options, writer);
2282+
const printer = createPrinter(options);
22832283
const sourceFile = enclosingDeclaration && getSourceFileOfNode(enclosingDeclaration);
22842284
printer.writeNode(EmitHint.Unspecified, typeNode, /*sourceFile*/ sourceFile, writer);
22852285
const result = writer.getText();
@@ -8668,6 +8668,7 @@ namespace ts {
86688668
let expandingFlags: number;
86698669
let depth = 0;
86708670
let overflow = false;
8671+
let dynamicDisableWeakTypeErrors = false;
86718672

86728673
Debug.assert(relation !== identityRelation || !errorNode, "no error reporting in identity checking");
86738674

@@ -8944,11 +8945,17 @@ namespace ts {
89448945
}
89458946
}
89468947

8947-
function typeRelatedToEachType(source: Type, target: UnionOrIntersectionType, reportErrors: boolean): Ternary {
8948+
function typeRelatedToEachType(source: Type, target: IntersectionType, reportErrors: boolean): Ternary {
89488949
let result = Ternary.True;
89498950
const targetTypes = target.types;
8951+
// disable weak-type detection for the main check
8952+
// unless all of target.types are weak, in which case run weak type detection on the *combined* properties of target
8953+
// at a minimum, do the first part.
8954+
// but the thing should be flexible enough to easily do the second part as a separate call
89508955
for (const targetType of targetTypes) {
8956+
dynamicDisableWeakTypeErrors = true;
89518957
const related = isRelatedTo(source, targetType, reportErrors);
8958+
dynamicDisableWeakTypeErrors = false;
89528959
if (!related) {
89538960
return Ternary.False;
89548961
}
@@ -9326,7 +9333,7 @@ namespace ts {
93269333
}
93279334
}
93289335
}
9329-
if (!foundMatchingProperty && getPropertiesOfType(source).length > 0) {
9336+
if (!foundMatchingProperty && !dynamicDisableWeakTypeErrors && getPropertiesOfType(source).length > 0) {
93309337
if (reportErrors) {
93319338
reportError(Diagnostics.Weak_type_0_has_no_properties_in_common_with_1, typeToString(target), typeToString(source));
93329339
}
@@ -10596,7 +10603,7 @@ namespace ts {
1059610603
let props = getPropertiesOfType(type);
1059710604
return type.flags & TypeFlags.Object &&
1059810605
props.length > 0 &&
10599-
!forEach(props, p => !(p.flags & SymbolFlags.Optional)) &&
10606+
every(props, p => !!(p.flags & SymbolFlags.Optional)) &&
1060010607
!getIndexTypeOfType(type, IndexKind.String) &&
1060110608
!getIndexTypeOfType(type, IndexKind.Number);
1060210609
}
@@ -22655,12 +22662,12 @@ namespace ts {
2265522662
return symbols;
2265622663
}
2265722664
else if (symbol.flags & SymbolFlags.Transient) {
22658-
if ((symbol as SymbolLinks).leftSpread) {
22659-
const links = symbol as SymbolLinks;
22660-
return [...getRootSymbols(links.leftSpread), ...getRootSymbols(links.rightSpread)];
22665+
const transient = symbol as TransientSymbol;
22666+
if (transient.leftSpread) {
22667+
return [...getRootSymbols(transient.leftSpread), ...getRootSymbols(transient.rightSpread)];
2266122668
}
22662-
if ((symbol as SymbolLinks).syntheticOrigin) {
22663-
return getRootSymbols((symbol as SymbolLinks).syntheticOrigin);
22669+
if (transient.syntheticOrigin) {
22670+
return getRootSymbols(transient.syntheticOrigin);
2266422671
}
2266522672

2266622673
let target: Symbol;

0 commit comments

Comments
 (0)