Skip to content

Commit 949956b

Browse files
authored
Improve multiple overloads error span (#32315)
* Improve multiple overloads error span When all errors for a multiple-overload error refer to the same span, use that span instead of the one for the entire call. This situation is quite common for 2-overload sets in React. * Update baselines * Fix lint
1 parent b0f050f commit 949956b

34 files changed

+154
-148
lines changed

src/compiler/checker.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21836,9 +21836,18 @@ namespace ts {
2183621836
}
2183721837

2183821838
const diags = max > 1 ? allDiagnostics[minIndex] : flatten(allDiagnostics);
21839-
const chain = map(diags, d => typeof d.messageText === "string" ? (d as DiagnosticMessageChain) : d.messageText);
21839+
Debug.assert(diags.length > 0, "No errors reported for 3 or fewer overload signatures");
21840+
const chain = chainDiagnosticMessages(
21841+
map(diags, d => typeof d.messageText === "string" ? (d as DiagnosticMessageChain) : d.messageText),
21842+
Diagnostics.No_overload_matches_this_call);
2184021843
const related = flatMap(diags, d => (d as Diagnostic).relatedInformation) as DiagnosticRelatedInformation[];
21841-
diagnostics.add(createDiagnosticForNodeFromMessageChain(node, chainDiagnosticMessages(chain, Diagnostics.No_overload_matches_this_call), related));
21844+
if (every(diags, d => d.start === diags[0].start && d.length === diags[0].length && d.file === diags[0].file)) {
21845+
const { file, start, length } = diags[0];
21846+
diagnostics.add({ file, start, length, code: chain.code, category: chain.category, messageText: chain, relatedInformation: related });
21847+
}
21848+
else {
21849+
diagnostics.add(createDiagnosticForNodeFromMessageChain(node, chain, related));
21850+
}
2184221851
}
2184321852
}
2184421853
else if (candidateForArgumentArityError) {

tests/baselines/reference/bigintWithLib.errors.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
tests/cases/compiler/bigintWithLib.ts(4,1): error TS2350: Only a void function can be called with the 'new' keyword.
2-
tests/cases/compiler/bigintWithLib.ts(16,15): error TS2769: No overload matches this call.
2+
tests/cases/compiler/bigintWithLib.ts(16,33): error TS2769: No overload matches this call.
33
Overload 1 of 3, '(length?: number): BigInt64Array', gave the following error.
44
Argument of type 'number[]' is not assignable to parameter of type 'number'.
55
Overload 2 of 3, '(array: Iterable<bigint>): BigInt64Array', gave the following error.
@@ -17,7 +17,7 @@ tests/cases/compiler/bigintWithLib.ts(16,15): error TS2769: No overload matches
1717
Argument of type 'number[]' is not assignable to parameter of type 'ArrayBuffer | SharedArrayBuffer'.
1818
Type 'number[]' is missing the following properties from type 'SharedArrayBuffer': byteLength, [Symbol.species], [Symbol.toStringTag]
1919
tests/cases/compiler/bigintWithLib.ts(21,13): error TS2540: Cannot assign to 'length' because it is a read-only property.
20-
tests/cases/compiler/bigintWithLib.ts(28,16): error TS2769: No overload matches this call.
20+
tests/cases/compiler/bigintWithLib.ts(28,35): error TS2769: No overload matches this call.
2121
Overload 1 of 3, '(length?: number): BigUint64Array', gave the following error.
2222
Argument of type 'number[]' is not assignable to parameter of type 'number'.
2323
Overload 2 of 3, '(array: Iterable<bigint>): BigUint64Array', gave the following error.
@@ -49,7 +49,7 @@ tests/cases/compiler/bigintWithLib.ts(43,26): error TS2345: Argument of type '12
4949
bigIntArray = new BigInt64Array(10);
5050
bigIntArray = new BigInt64Array([1n, 2n, 3n]);
5151
bigIntArray = new BigInt64Array([1, 2, 3]); // should error
52-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
52+
~~~~~~~~~
5353
!!! error TS2769: No overload matches this call.
5454
!!! error TS2769: Overload 1 of 3, '(length?: number): BigInt64Array', gave the following error.
5555
!!! error TS2769: Argument of type 'number[]' is not assignable to parameter of type 'number'.
@@ -81,7 +81,7 @@ tests/cases/compiler/bigintWithLib.ts(43,26): error TS2345: Argument of type '12
8181
bigUintArray = new BigUint64Array(10);
8282
bigUintArray = new BigUint64Array([1n, 2n, 3n]);
8383
bigUintArray = new BigUint64Array([1, 2, 3]); // should error
84-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
84+
~~~~~~~~~
8585
!!! error TS2769: No overload matches this call.
8686
!!! error TS2769: Overload 1 of 3, '(length?: number): BigUint64Array', gave the following error.
8787
!!! error TS2769: Argument of type 'number[]' is not assignable to parameter of type 'number'.

tests/baselines/reference/checkJsxChildrenCanBeTupleType.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
tests/cases/conformance/jsx/checkJsxChildrenCanBeTupleType.tsx(17,17): error TS2769: No overload matches this call.
1+
tests/cases/conformance/jsx/checkJsxChildrenCanBeTupleType.tsx(17,18): error TS2769: No overload matches this call.
22
Overload 1 of 2, '(props: Readonly<ResizablePanelProps>): ResizablePanel', gave the following error.
33
Type '{ children: [Element, Element, Element]; }' is not assignable to type 'Readonly<ResizablePanelProps>'.
44
Types of property 'children' are incompatible.
@@ -29,7 +29,7 @@ tests/cases/conformance/jsx/checkJsxChildrenCanBeTupleType.tsx(17,17): error TS2
2929
</ResizablePanel>
3030

3131
const testErr = <ResizablePanel>
32-
~~~~~~~~~~~~~~~~
32+
~~~~~~~~~~~~~~
3333
!!! error TS2769: No overload matches this call.
3434
!!! error TS2769: Overload 1 of 2, '(props: Readonly<ResizablePanelProps>): ResizablePanel', gave the following error.
3535
!!! error TS2769: Type '{ children: [Element, Element, Element]; }' is not assignable to type 'Readonly<ResizablePanelProps>'.

tests/baselines/reference/constructorOverloads1.errors.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ tests/cases/compiler/constructorOverloads1.ts(2,5): error TS2392: Multiple const
22
tests/cases/compiler/constructorOverloads1.ts(3,5): error TS2392: Multiple constructor implementations are not allowed.
33
tests/cases/compiler/constructorOverloads1.ts(4,5): error TS2392: Multiple constructor implementations are not allowed.
44
tests/cases/compiler/constructorOverloads1.ts(7,5): error TS2392: Multiple constructor implementations are not allowed.
5-
tests/cases/compiler/constructorOverloads1.ts(16,10): error TS2769: No overload matches this call.
5+
tests/cases/compiler/constructorOverloads1.ts(16,18): error TS2769: No overload matches this call.
66
Overload 1 of 2, '(s: string): Foo', gave the following error.
77
Argument of type 'Foo' is not assignable to parameter of type 'string'.
88
Overload 2 of 2, '(n: number): Foo', gave the following error.
99
Argument of type 'Foo' is not assignable to parameter of type 'number'.
10-
tests/cases/compiler/constructorOverloads1.ts(17,10): error TS2769: No overload matches this call.
10+
tests/cases/compiler/constructorOverloads1.ts(17,18): error TS2769: No overload matches this call.
1111
Overload 1 of 2, '(s: string): Foo', gave the following error.
1212
Argument of type 'any[]' is not assignable to parameter of type 'string'.
1313
Overload 2 of 2, '(n: number): Foo', gave the following error.
@@ -43,14 +43,14 @@ tests/cases/compiler/constructorOverloads1.ts(17,10): error TS2769: No overload
4343
var f1 = new Foo("hey");
4444
var f2 = new Foo(0);
4545
var f3 = new Foo(f1);
46-
~~~~~~~~~~~
46+
~~
4747
!!! error TS2769: No overload matches this call.
4848
!!! error TS2769: Overload 1 of 2, '(s: string): Foo', gave the following error.
4949
!!! error TS2769: Argument of type 'Foo' is not assignable to parameter of type 'string'.
5050
!!! error TS2769: Overload 2 of 2, '(n: number): Foo', gave the following error.
5151
!!! error TS2769: Argument of type 'Foo' is not assignable to parameter of type 'number'.
5252
var f4 = new Foo([f1,f2,f3]);
53-
~~~~~~~~~~~~~~~~~~~
53+
~~~~~~~~~~
5454
!!! error TS2769: No overload matches this call.
5555
!!! error TS2769: Overload 1 of 2, '(s: string): Foo', gave the following error.
5656
!!! error TS2769: Argument of type 'any[]' is not assignable to parameter of type 'string'.

tests/baselines/reference/contextuallyTypedStringLiteralsInJsxAttributes02.errors.txt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
1-
tests/cases/conformance/types/contextualTypes/jsxAttributes/file.tsx(27,12): error TS2769: No overload matches this call.
1+
tests/cases/conformance/types/contextualTypes/jsxAttributes/file.tsx(27,13): error TS2769: No overload matches this call.
22
Overload 1 of 2, '(buttonProps: ButtonProps): Element', gave the following error.
33
Type '{ extra: true; onClick: (k: "left" | "right") => void; }' is not assignable to type 'IntrinsicAttributes & ButtonProps'.
44
Property 'extra' does not exist on type 'IntrinsicAttributes & ButtonProps'.
55
Overload 2 of 2, '(linkProps: LinkProps): Element', gave the following error.
66
Type '{ extra: true; onClick: (k: "left" | "right") => void; }' is not assignable to type 'IntrinsicAttributes & LinkProps'.
77
Property 'extra' does not exist on type 'IntrinsicAttributes & LinkProps'.
8-
tests/cases/conformance/types/contextualTypes/jsxAttributes/file.tsx(28,12): error TS2769: No overload matches this call.
8+
tests/cases/conformance/types/contextualTypes/jsxAttributes/file.tsx(28,13): error TS2769: No overload matches this call.
99
Overload 1 of 2, '(buttonProps: ButtonProps): Element', gave the following error.
1010
Type '{ onClick: (k: "left" | "right") => void; extra: true; }' is not assignable to type 'IntrinsicAttributes & ButtonProps'.
1111
Property 'extra' does not exist on type 'IntrinsicAttributes & ButtonProps'.
1212
Overload 2 of 2, '(linkProps: LinkProps): Element', gave the following error.
1313
Type '{ onClick: (k: "left" | "right") => void; extra: true; }' is not assignable to type 'IntrinsicAttributes & LinkProps'.
1414
Property 'onClick' does not exist on type 'IntrinsicAttributes & LinkProps'.
15-
tests/cases/conformance/types/contextualTypes/jsxAttributes/file.tsx(29,12): error TS2769: No overload matches this call.
15+
tests/cases/conformance/types/contextualTypes/jsxAttributes/file.tsx(29,13): error TS2769: No overload matches this call.
1616
Overload 1 of 2, '(buttonProps: ButtonProps): Element', gave the following error.
1717
Type '{ extra: true; goTo: string; }' is not assignable to type 'IntrinsicAttributes & ButtonProps'.
1818
Property 'extra' does not exist on type 'IntrinsicAttributes & ButtonProps'.
1919
Overload 2 of 2, '(linkProps: LinkProps): Element', gave the following error.
2020
Type '{ extra: true; goTo: "home"; }' is not assignable to type 'IntrinsicAttributes & LinkProps'.
2121
Property 'extra' does not exist on type 'IntrinsicAttributes & LinkProps'.
22-
tests/cases/conformance/types/contextualTypes/jsxAttributes/file.tsx(30,12): error TS2769: No overload matches this call.
22+
tests/cases/conformance/types/contextualTypes/jsxAttributes/file.tsx(30,13): error TS2769: No overload matches this call.
2323
Overload 1 of 2, '(buttonProps: ButtonProps): Element', gave the following error.
2424
Type '{ goTo: string; extra: true; }' is not assignable to type 'IntrinsicAttributes & ButtonProps'.
2525
Property 'goTo' does not exist on type 'IntrinsicAttributes & ButtonProps'.
@@ -60,7 +60,7 @@ tests/cases/conformance/types/contextualTypes/jsxAttributes/file.tsx(36,13): err
6060
}
6161

6262
const b0 = <MainButton {...{onClick: (k) => {console.log(k)}}} extra />; // k has type "left" | "right"
63-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
63+
~~~~~~~~~~
6464
!!! error TS2769: No overload matches this call.
6565
!!! error TS2769: Overload 1 of 2, '(buttonProps: ButtonProps): Element', gave the following error.
6666
!!! error TS2769: Type '{ extra: true; onClick: (k: "left" | "right") => void; }' is not assignable to type 'IntrinsicAttributes & ButtonProps'.
@@ -69,7 +69,7 @@ tests/cases/conformance/types/contextualTypes/jsxAttributes/file.tsx(36,13): err
6969
!!! error TS2769: Type '{ extra: true; onClick: (k: "left" | "right") => void; }' is not assignable to type 'IntrinsicAttributes & LinkProps'.
7070
!!! error TS2769: Property 'extra' does not exist on type 'IntrinsicAttributes & LinkProps'.
7171
const b2 = <MainButton onClick={(k)=>{console.log(k)}} extra />; // k has type "left" | "right"
72-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
72+
~~~~~~~~~~
7373
!!! error TS2769: No overload matches this call.
7474
!!! error TS2769: Overload 1 of 2, '(buttonProps: ButtonProps): Element', gave the following error.
7575
!!! error TS2769: Type '{ onClick: (k: "left" | "right") => void; extra: true; }' is not assignable to type 'IntrinsicAttributes & ButtonProps'.
@@ -78,7 +78,7 @@ tests/cases/conformance/types/contextualTypes/jsxAttributes/file.tsx(36,13): err
7878
!!! error TS2769: Type '{ onClick: (k: "left" | "right") => void; extra: true; }' is not assignable to type 'IntrinsicAttributes & LinkProps'.
7979
!!! error TS2769: Property 'onClick' does not exist on type 'IntrinsicAttributes & LinkProps'.
8080
const b3 = <MainButton {...{goTo:"home"}} extra />; // goTo has type"home" | "contact"
81-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
81+
~~~~~~~~~~
8282
!!! error TS2769: No overload matches this call.
8383
!!! error TS2769: Overload 1 of 2, '(buttonProps: ButtonProps): Element', gave the following error.
8484
!!! error TS2769: Type '{ extra: true; goTo: string; }' is not assignable to type 'IntrinsicAttributes & ButtonProps'.
@@ -87,7 +87,7 @@ tests/cases/conformance/types/contextualTypes/jsxAttributes/file.tsx(36,13): err
8787
!!! error TS2769: Type '{ extra: true; goTo: "home"; }' is not assignable to type 'IntrinsicAttributes & LinkProps'.
8888
!!! error TS2769: Property 'extra' does not exist on type 'IntrinsicAttributes & LinkProps'.
8989
const b4 = <MainButton goTo="home" extra />; // goTo has type "home" | "contact"
90-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
90+
~~~~~~~~~~
9191
!!! error TS2769: No overload matches this call.
9292
!!! error TS2769: Overload 1 of 2, '(buttonProps: ButtonProps): Element', gave the following error.
9393
!!! error TS2769: Type '{ goTo: string; extra: true; }' is not assignable to type 'IntrinsicAttributes & ButtonProps'.

tests/baselines/reference/controlFlowIterationErrors.errors.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ tests/cases/conformance/controlFlow/controlFlowIterationErrors.ts(11,17): error
22
Type 'number' is not assignable to type 'string'.
33
tests/cases/conformance/controlFlow/controlFlowIterationErrors.ts(22,17): error TS2345: Argument of type 'string | number' is not assignable to parameter of type 'string'.
44
Type 'number' is not assignable to type 'string'.
5-
tests/cases/conformance/controlFlow/controlFlowIterationErrors.ts(34,13): error TS2769: No overload matches this call.
5+
tests/cases/conformance/controlFlow/controlFlowIterationErrors.ts(34,17): error TS2769: No overload matches this call.
66
Overload 1 of 2, '(x: string): number', gave the following error.
77
Argument of type 'string | number' is not assignable to parameter of type 'string'.
88
Type 'number' is not assignable to type 'string'.
99
Overload 2 of 2, '(x: number): string', gave the following error.
1010
Argument of type 'string | number' is not assignable to parameter of type 'number'.
1111
Type 'string' is not assignable to type 'number'.
12-
tests/cases/conformance/controlFlow/controlFlowIterationErrors.ts(45,13): error TS2769: No overload matches this call.
12+
tests/cases/conformance/controlFlow/controlFlowIterationErrors.ts(45,17): error TS2769: No overload matches this call.
1313
Overload 1 of 2, '(x: string): number', gave the following error.
1414
Argument of type 'string | number' is not assignable to parameter of type 'string'.
1515
Type 'number' is not assignable to type 'string'.
@@ -59,7 +59,7 @@ tests/cases/conformance/controlFlow/controlFlowIterationErrors.ts(45,13): error
5959
x = "";
6060
while (cond) {
6161
x = foo(x);
62-
~~~~~~
62+
~
6363
!!! error TS2769: No overload matches this call.
6464
!!! error TS2769: Overload 1 of 2, '(x: string): number', gave the following error.
6565
!!! error TS2769: Argument of type 'string | number' is not assignable to parameter of type 'string'.
@@ -78,7 +78,7 @@ tests/cases/conformance/controlFlow/controlFlowIterationErrors.ts(45,13): error
7878
while (cond) {
7979
x;
8080
x = foo(x);
81-
~~~~~~
81+
~
8282
!!! error TS2769: No overload matches this call.
8383
!!! error TS2769: Overload 1 of 2, '(x: string): number', gave the following error.
8484
!!! error TS2769: Argument of type 'string | number' is not assignable to parameter of type 'string'.

tests/baselines/reference/destructuringTuple.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
tests/cases/compiler/destructuringTuple.ts(11,8): error TS2493: Tuple type '[]' of length '0' has no element at index '0'.
2-
tests/cases/compiler/destructuringTuple.ts(11,48): error TS2769: No overload matches this call.
2+
tests/cases/compiler/destructuringTuple.ts(11,60): error TS2769: No overload matches this call.
33
Overload 1 of 2, '(...items: ConcatArray<never>[]): never[]', gave the following error.
44
Argument of type 'number' is not assignable to parameter of type 'ConcatArray<never>'.
55
Overload 2 of 2, '(...items: ConcatArray<never>[]): never[]', gave the following error.
@@ -20,7 +20,7 @@ tests/cases/compiler/destructuringTuple.ts(11,48): error TS2769: No overload mat
2020
const [oops1] = [1, 2, 3].reduce((accu, el) => accu.concat(el), []);
2121
~~~~~
2222
!!! error TS2493: Tuple type '[]' of length '0' has no element at index '0'.
23-
~~~~~~~~~~~~~~~
23+
~~
2424
!!! error TS2769: No overload matches this call.
2525
!!! error TS2769: Overload 1 of 2, '(...items: ConcatArray<never>[]): never[]', gave the following error.
2626
!!! error TS2769: Argument of type 'number' is not assignable to parameter of type 'ConcatArray<never>'.

tests/baselines/reference/functionOverloads2.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
tests/cases/compiler/functionOverloads2.ts(4,9): error TS2769: No overload matches this call.
1+
tests/cases/compiler/functionOverloads2.ts(4,13): error TS2769: No overload matches this call.
22
Overload 1 of 2, '(bar: string): string', gave the following error.
33
Argument of type 'true' is not assignable to parameter of type 'string'.
44
Overload 2 of 2, '(bar: number): number', gave the following error.
@@ -10,7 +10,7 @@ tests/cases/compiler/functionOverloads2.ts(4,9): error TS2769: No overload match
1010
function foo(bar: number): number;
1111
function foo(bar: any): any { return bar };
1212
var x = foo(true);
13-
~~~~~~~~~
13+
~~~~
1414
!!! error TS2769: No overload matches this call.
1515
!!! error TS2769: Overload 1 of 2, '(bar: string): string', gave the following error.
1616
!!! error TS2769: Argument of type 'true' is not assignable to parameter of type 'string'.

tests/baselines/reference/functionOverloads40.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
tests/cases/compiler/functionOverloads40.ts(4,9): error TS2769: No overload matches this call.
1+
tests/cases/compiler/functionOverloads40.ts(4,15): error TS2769: No overload matches this call.
22
Overload 1 of 2, '(bar: { a: number; }[]): string', gave the following error.
33
Type 'string' is not assignable to type 'number'.
44
Overload 2 of 2, '(bar: { a: boolean; }[]): number', gave the following error.
@@ -10,7 +10,7 @@ tests/cases/compiler/functionOverloads40.ts(4,9): error TS2769: No overload matc
1010
function foo(bar:{a:boolean;}[]):number;
1111
function foo(bar:{a:any;}[]):any{ return bar }
1212
var x = foo([{a:'bar'}]);
13-
~~~~~~~~~~~~~~~~
13+
~
1414
!!! error TS2769: No overload matches this call.
1515
!!! error TS2769: Overload 1 of 2, '(bar: { a: number; }[]): string', gave the following error.
1616
!!! error TS2769: Type 'string' is not assignable to type 'number'.

0 commit comments

Comments
 (0)