Skip to content

Commit 865ec14

Browse files
authored
Don't suppress insufficient type overlap diagnostics (#1746)
1 parent 7a33193 commit 865ec14

16 files changed

+77
-245
lines changed

internal/checker/checker.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2328,7 +2328,7 @@ func (c *Checker) checkDeferredNode(node *ast.Node) {
23282328
c.checkJsxSelfClosingElementDeferred(node)
23292329
case ast.KindJsxElement:
23302330
c.checkJsxElementDeferred(node)
2331-
case ast.KindTypeAssertionExpression, ast.KindAsExpression, ast.KindParenthesizedExpression:
2331+
case ast.KindTypeAssertionExpression, ast.KindAsExpression:
23322332
c.checkAssertionDeferred(node)
23332333
case ast.KindVoidExpression:
23342334
c.checkExpression(node.AsVoidExpression().Expression)
@@ -11799,7 +11799,11 @@ func (c *Checker) checkAssertionDeferred(node *ast.Node) {
1179911799
if !c.isErrorType(targetType) {
1180011800
widenedType := c.getWidenedType(exprType)
1180111801
if !c.isTypeComparableTo(targetType, widenedType) {
11802-
c.checkTypeComparableTo(exprType, targetType, node, diagnostics.Conversion_of_type_0_to_type_1_may_be_a_mistake_because_neither_type_sufficiently_overlaps_with_the_other_If_this_was_intentional_convert_the_expression_to_unknown_first)
11802+
errNode := node
11803+
if node.Flags&ast.NodeFlagsReparsed != 0 {
11804+
errNode = node.Type()
11805+
}
11806+
c.checkTypeComparableTo(exprType, targetType, errNode, diagnostics.Conversion_of_type_0_to_type_1_may_be_a_mistake_because_neither_type_sufficiently_overlaps_with_the_other_If_this_was_intentional_convert_the_expression_to_unknown_first)
1180311807
}
1180411808
}
1180511809
}

internal/checker/relater.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4728,16 +4728,16 @@ func (r *Relater) reportRelationError(message *diagnostics.Message, source *Type
47284728
return
47294729
}
47304730
// Suppress if next message is a missing property message for source and target and we're not
4731-
// reporting on interface implementation
4731+
// reporting on conversion or interface implementation
47324732
case diagnostics.Property_0_is_missing_in_type_1_but_required_in_type_2:
4733-
if !isInterfaceImplementationMessage(message) && r.chainArgsMatch(nil, generalizedSourceType, targetType) {
4733+
if !isConversionOrInterfaceImplementationMessage(message) && r.chainArgsMatch(nil, generalizedSourceType, targetType) {
47344734
return
47354735
}
47364736
// Suppress if next message is a missing property message for source and target and we're not
4737-
// reporting on interface implementation
4737+
// reporting on conversion or interface implementation
47384738
case diagnostics.Type_0_is_missing_the_following_properties_from_type_1_Colon_2_and_3_more,
47394739
diagnostics.Type_0_is_missing_the_following_properties_from_type_1_Colon_2:
4740-
if !isInterfaceImplementationMessage(message) && r.chainArgsMatch(generalizedSourceType, targetType) {
4740+
if !isConversionOrInterfaceImplementationMessage(message) && r.chainArgsMatch(generalizedSourceType, targetType) {
47414741
return
47424742
}
47434743
}
@@ -4847,9 +4847,10 @@ func getPropertyNameArg(arg any) string {
48474847
return s
48484848
}
48494849

4850-
func isInterfaceImplementationMessage(message *diagnostics.Message) bool {
4850+
func isConversionOrInterfaceImplementationMessage(message *diagnostics.Message) bool {
48514851
return message == diagnostics.Class_0_incorrectly_implements_interface_1 ||
4852-
message == diagnostics.Class_0_incorrectly_implements_class_1_Did_you_mean_to_extend_1_and_inherit_its_members_as_a_subclass
4852+
message == diagnostics.Class_0_incorrectly_implements_class_1_Did_you_mean_to_extend_1_and_inherit_its_members_as_a_subclass ||
4853+
message == diagnostics.Conversion_of_type_0_to_type_1_may_be_a_mistake_because_neither_type_sufficiently_overlaps_with_the_other_If_this_was_intentional_convert_the_expression_to_unknown_first
48534854
}
48544855

48554856
func chainDepth(chain *ErrorChain) int {

testdata/baselines/reference/submodule/compiler/fuzzy.errors.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ fuzzy.ts(13,18): error TS2420: Class 'C' incorrectly implements interface 'I'.
22
Property 'alsoWorks' is missing in type 'C' but required in type 'I'.
33
fuzzy.ts(21,34): error TS2322: Type 'this' is not assignable to type 'I'.
44
Property 'alsoWorks' is missing in type 'C' but required in type 'I'.
5-
fuzzy.ts(25,20): error TS2741: Property 'anything' is missing in type '{ oneI: this; }' but required in type 'R'.
5+
fuzzy.ts(25,20): error TS2352: Conversion of type '{ oneI: this; }' to type 'R' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
6+
Property 'anything' is missing in type '{ oneI: this; }' but required in type 'R'.
67

78

89
==== fuzzy.ts (3 errors) ====
@@ -41,7 +42,8 @@ fuzzy.ts(25,20): error TS2741: Property 'anything' is missing in type '{ oneI: t
4142
worksToo():R {
4243
return <R>({ oneI: this });
4344
~~~~~~~~~~~~~~~~~~~
44-
!!! error TS2741: Property 'anything' is missing in type '{ oneI: this; }' but required in type 'R'.
45+
!!! error TS2352: Conversion of type '{ oneI: this; }' to type 'R' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
46+
!!! error TS2352: Property 'anything' is missing in type '{ oneI: this; }' but required in type 'R'.
4547
!!! related TS2728 fuzzy.ts:9:9: 'anything' is declared here.
4648
}
4749
}

testdata/baselines/reference/submodule/compiler/fuzzy.errors.txt.diff

Lines changed: 0 additions & 22 deletions
This file was deleted.

testdata/baselines/reference/submodule/compiler/genericTypeAssertions2.errors.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ genericTypeAssertions2.ts(10,5): error TS2322: Type 'B<string>' is not assignabl
44
Types of parameters 'x' and 'x' are incompatible.
55
Type 'number' is not assignable to type 'string'.
66
genericTypeAssertions2.ts(11,5): error TS2741: Property 'bar' is missing in type 'A<number>' but required in type 'B<number>'.
7-
genericTypeAssertions2.ts(13,21): error TS2741: Property 'foo' is missing in type 'undefined[]' but required in type 'A<number>'.
7+
genericTypeAssertions2.ts(13,21): error TS2352: Conversion of type 'undefined[]' to type 'A<number>' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
8+
Property 'foo' is missing in type 'undefined[]' but required in type 'A<number>'.
89

910

1011
==== genericTypeAssertions2.ts (3 errors) ====
@@ -31,5 +32,6 @@ genericTypeAssertions2.ts(13,21): error TS2741: Property 'foo' is missing in typ
3132
var r4: A<number> = <A<number>>new A();
3233
var r5: A<number> = <A<number>>[]; // error
3334
~~~~~~~~~~~~~
34-
!!! error TS2741: Property 'foo' is missing in type 'undefined[]' but required in type 'A<number>'.
35+
!!! error TS2352: Conversion of type 'undefined[]' to type 'A<number>' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
36+
!!! error TS2352: Property 'foo' is missing in type 'undefined[]' but required in type 'A<number>'.
3537
!!! related TS2728 genericTypeAssertions2.ts:1:14: 'foo' is declared here.

testdata/baselines/reference/submodule/compiler/genericTypeAssertions2.errors.txt.diff

Lines changed: 0 additions & 20 deletions
This file was deleted.

testdata/baselines/reference/submodule/compiler/noImplicitAnyInCastExpression.errors.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
noImplicitAnyInCastExpression.ts(15,2): error TS2739: Type '{ c: null; }' is missing the following properties from type 'IFoo': a, b
1+
noImplicitAnyInCastExpression.ts(15,2): error TS2352: Conversion of type '{ c: null; }' to type 'IFoo' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
2+
Type '{ c: null; }' is missing the following properties from type 'IFoo': a, b
23

34

45
==== noImplicitAnyInCastExpression.ts (1 errors) ====
@@ -18,4 +19,5 @@ noImplicitAnyInCastExpression.ts(15,2): error TS2739: Type '{ c: null; }' is mis
1819
// Neither types is assignable to each other
1920
(<IFoo>{ c: null });
2021
~~~~~~~~~~~~~~~~~
21-
!!! error TS2739: Type '{ c: null; }' is missing the following properties from type 'IFoo': a, b
22+
!!! error TS2352: Conversion of type '{ c: null; }' to type 'IFoo' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
23+
!!! error TS2352: Type '{ c: null; }' is missing the following properties from type 'IFoo': a, b

testdata/baselines/reference/submodule/compiler/noImplicitAnyInCastExpression.errors.txt.diff

Lines changed: 0 additions & 16 deletions
This file was deleted.

testdata/baselines/reference/submodule/conformance/jsdocTypeTagCast.errors.txt

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
b.js(4,31): error TS2352: Conversion of type 'number' to type 'string' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
2-
b.js(45,36): error TS2741: Property 'p' is missing in type 'SomeOther' but required in type 'SomeBase'.
3-
b.js(49,42): error TS2739: Type 'SomeOther' is missing the following properties from type 'SomeDerived': p, x
4-
b.js(51,38): error TS2741: Property 'q' is missing in type 'SomeDerived' but required in type 'SomeOther'.
5-
b.js(52,38): error TS2741: Property 'q' is missing in type 'SomeBase' but required in type 'SomeOther'.
1+
b.js(4,20): error TS2352: Conversion of type 'number' to type 'string' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
2+
b.js(45,23): error TS2352: Conversion of type 'SomeOther' to type 'SomeBase' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
3+
Property 'p' is missing in type 'SomeOther' but required in type 'SomeBase'.
4+
b.js(49,26): error TS2352: Conversion of type 'SomeOther' to type 'SomeDerived' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
5+
Type 'SomeOther' is missing the following properties from type 'SomeDerived': p, x
6+
b.js(51,24): error TS2352: Conversion of type 'SomeDerived' to type 'SomeOther' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
7+
Property 'q' is missing in type 'SomeDerived' but required in type 'SomeOther'.
8+
b.js(52,24): error TS2352: Conversion of type 'SomeBase' to type 'SomeOther' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
9+
Property 'q' is missing in type 'SomeBase' but required in type 'SomeOther'.
610
b.js(66,15): error TS1228: A type predicate is only allowed in return type position for functions and methods.
711
b.js(66,38): error TS2454: Variable 'numOrStr' is used before being assigned.
812
b.js(67,2): error TS2322: Type 'string | number' is not assignable to type 'string'.
@@ -18,7 +22,7 @@ b.js(67,8): error TS2454: Variable 'numOrStr' is used before being assigned.
1822
var W = /** @type {string} */(/** @type {*} */ (4));
1923

2024
var W = /** @type {string} */(4); // Error
21-
~
25+
~~~~~~
2226
!!! error TS2352: Conversion of type 'number' to type 'string' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
2327

2428
/** @type {*} */
@@ -61,23 +65,27 @@ b.js(67,8): error TS2454: Variable 'numOrStr' is used before being assigned.
6165
someBase = /** @type {SomeBase} */(someDerived);
6266
someBase = /** @type {SomeBase} */(someBase);
6367
someBase = /** @type {SomeBase} */(someOther); // Error
64-
~~~~~~~~~
65-
!!! error TS2741: Property 'p' is missing in type 'SomeOther' but required in type 'SomeBase'.
68+
~~~~~~~~
69+
!!! error TS2352: Conversion of type 'SomeOther' to type 'SomeBase' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
70+
!!! error TS2352: Property 'p' is missing in type 'SomeOther' but required in type 'SomeBase'.
6671
!!! related TS2728 b.js:17:9: 'p' is declared here.
6772

6873
someDerived = /** @type {SomeDerived} */(someDerived);
6974
someDerived = /** @type {SomeDerived} */(someBase);
7075
someDerived = /** @type {SomeDerived} */(someOther); // Error
71-
~~~~~~~~~
72-
!!! error TS2739: Type 'SomeOther' is missing the following properties from type 'SomeDerived': p, x
76+
~~~~~~~~~~~
77+
!!! error TS2352: Conversion of type 'SomeOther' to type 'SomeDerived' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
78+
!!! error TS2352: Type 'SomeOther' is missing the following properties from type 'SomeDerived': p, x
7379

7480
someOther = /** @type {SomeOther} */(someDerived); // Error
75-
~~~~~~~~~~~
76-
!!! error TS2741: Property 'q' is missing in type 'SomeDerived' but required in type 'SomeOther'.
81+
~~~~~~~~~
82+
!!! error TS2352: Conversion of type 'SomeDerived' to type 'SomeOther' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
83+
!!! error TS2352: Property 'q' is missing in type 'SomeDerived' but required in type 'SomeOther'.
7784
!!! related TS2728 b.js:28:9: 'q' is declared here.
7885
someOther = /** @type {SomeOther} */(someBase); // Error
79-
~~~~~~~~
80-
!!! error TS2741: Property 'q' is missing in type 'SomeBase' but required in type 'SomeOther'.
86+
~~~~~~~~~
87+
!!! error TS2352: Conversion of type 'SomeBase' to type 'SomeOther' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
88+
!!! error TS2352: Property 'q' is missing in type 'SomeBase' but required in type 'SomeOther'.
8189
!!! related TS2728 b.js:28:9: 'q' is declared here.
8290
someOther = /** @type {SomeOther} */(someOther);
8391

testdata/baselines/reference/submodule/conformance/objectTypesIdentityWithPrivates3.errors.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
objectTypesIdentityWithPrivates3.ts(25,1): error TS2741: Property 'y' is missing in type 'C3<T2>' but required in type 'C4'.
1+
objectTypesIdentityWithPrivates3.ts(25,1): error TS2352: Conversion of type 'C3<T2>' to type 'C4' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
2+
Property 'y' is missing in type 'C3<T2>' but required in type 'C4'.
23

34

45
==== objectTypesIdentityWithPrivates3.ts (1 errors) ====
@@ -28,5 +29,6 @@ objectTypesIdentityWithPrivates3.ts(25,1): error TS2741: Property 'y' is missing
2829
var c3: C3<T2>;
2930
<C4>c3; // Should fail (private x originates in the same declaration, but different types)
3031
~~~~~~
31-
!!! error TS2741: Property 'y' is missing in type 'C3<T2>' but required in type 'C4'.
32+
!!! error TS2352: Conversion of type 'C3<T2>' to type 'C4' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
33+
!!! error TS2352: Property 'y' is missing in type 'C3<T2>' but required in type 'C4'.
3234
!!! related TS2728 objectTypesIdentityWithPrivates3.ts:21:5: 'y' is declared here.

0 commit comments

Comments
 (0)