Skip to content

Commit bc07eec

Browse files
Added tests for new error messages on calls to unions.
1 parent 768318b commit bc07eec

File tree

6 files changed

+112
-1
lines changed

6 files changed

+112
-1
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22090,7 +22090,7 @@ namespace ts {
2209022090
);
2209122091
}
2209222092
function invocationError(node: Node, apparentType: Type, kind: SignatureKind, relatedInformation?: DiagnosticRelatedInformation) {
22093-
const diagnostic: Diagnostic = createDiagnosticForNodeFromMessageChain(node, invocationErrorDetails(apparentType, kind));
22093+
const diagnostic = createDiagnosticForNodeFromMessageChain(node, invocationErrorDetails(apparentType, kind));
2209422094
diagnostics.add(diagnostic);
2209522095
invocationErrorRecovery(apparentType, kind, relatedInformation ? addRelatedInfo(diagnostic, relatedInformation) : diagnostic);
2209622096
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
tests/cases/compiler/betterErrorForUnionCall.ts(2,1): error TS2349: This expression is not callable.
2+
No constituent of type '{ a: string; } | { b: string; }' is callable.
3+
tests/cases/compiler/betterErrorForUnionCall.ts(5,1): error TS2349: This expression is not callable.
4+
Not all constituents of type '{ a: string; } | ((a: string) => void)' are callable.
5+
Type '{ a: string; }' has no call signatures.
6+
tests/cases/compiler/betterErrorForUnionCall.ts(8,1): error TS2349: This expression is not callable.
7+
Each member of the union type '(<T extends number>(a: T) => void) | (<T>(a: string) => void)' has signatures, but none of those signatures are compatible with each other.
8+
9+
10+
==== tests/cases/compiler/betterErrorForUnionCall.ts (3 errors) ====
11+
declare const union: { a: string } | { b: string }
12+
union("");
13+
~~~~~~~~~
14+
!!! error TS2349: This expression is not callable.
15+
!!! error TS2349: No constituent of type '{ a: string; } | { b: string; }' is callable.
16+
17+
declare const fnUnion: { a: string } | ((a: string) => void)
18+
fnUnion("");
19+
~~~~~~~~~~~
20+
!!! error TS2349: This expression is not callable.
21+
!!! error TS2349: Not all constituents of type '{ a: string; } | ((a: string) => void)' are callable.
22+
!!! error TS2349: Type '{ a: string; }' has no call signatures.
23+
24+
declare const fnUnion2: (<T extends number>(a: T) => void) | (<T>(a: string) => void)
25+
fnUnion2("");
26+
~~~~~~~~~~~~
27+
!!! error TS2349: This expression is not callable.
28+
!!! error TS2349: Each member of the union type '(<T extends number>(a: T) => void) | (<T>(a: string) => void)' has signatures, but none of those signatures are compatible with each other.
29+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//// [betterErrorForUnionCall.ts]
2+
declare const union: { a: string } | { b: string }
3+
union("");
4+
5+
declare const fnUnion: { a: string } | ((a: string) => void)
6+
fnUnion("");
7+
8+
declare const fnUnion2: (<T extends number>(a: T) => void) | (<T>(a: string) => void)
9+
fnUnion2("");
10+
11+
12+
//// [betterErrorForUnionCall.js]
13+
union("");
14+
fnUnion("");
15+
fnUnion2("");
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
=== tests/cases/compiler/betterErrorForUnionCall.ts ===
2+
declare const union: { a: string } | { b: string }
3+
>union : Symbol(union, Decl(betterErrorForUnionCall.ts, 0, 13))
4+
>a : Symbol(a, Decl(betterErrorForUnionCall.ts, 0, 22))
5+
>b : Symbol(b, Decl(betterErrorForUnionCall.ts, 0, 38))
6+
7+
union("");
8+
>union : Symbol(union, Decl(betterErrorForUnionCall.ts, 0, 13))
9+
10+
declare const fnUnion: { a: string } | ((a: string) => void)
11+
>fnUnion : Symbol(fnUnion, Decl(betterErrorForUnionCall.ts, 3, 13))
12+
>a : Symbol(a, Decl(betterErrorForUnionCall.ts, 3, 24))
13+
>a : Symbol(a, Decl(betterErrorForUnionCall.ts, 3, 41))
14+
15+
fnUnion("");
16+
>fnUnion : Symbol(fnUnion, Decl(betterErrorForUnionCall.ts, 3, 13))
17+
18+
declare const fnUnion2: (<T extends number>(a: T) => void) | (<T>(a: string) => void)
19+
>fnUnion2 : Symbol(fnUnion2, Decl(betterErrorForUnionCall.ts, 6, 13))
20+
>T : Symbol(T, Decl(betterErrorForUnionCall.ts, 6, 26))
21+
>a : Symbol(a, Decl(betterErrorForUnionCall.ts, 6, 44))
22+
>T : Symbol(T, Decl(betterErrorForUnionCall.ts, 6, 26))
23+
>T : Symbol(T, Decl(betterErrorForUnionCall.ts, 6, 63))
24+
>a : Symbol(a, Decl(betterErrorForUnionCall.ts, 6, 66))
25+
26+
fnUnion2("");
27+
>fnUnion2 : Symbol(fnUnion2, Decl(betterErrorForUnionCall.ts, 6, 13))
28+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
=== tests/cases/compiler/betterErrorForUnionCall.ts ===
2+
declare const union: { a: string } | { b: string }
3+
>union : { a: string; } | { b: string; }
4+
>a : string
5+
>b : string
6+
7+
union("");
8+
>union("") : any
9+
>union : { a: string; } | { b: string; }
10+
>"" : ""
11+
12+
declare const fnUnion: { a: string } | ((a: string) => void)
13+
>fnUnion : { a: string; } | ((a: string) => void)
14+
>a : string
15+
>a : string
16+
17+
fnUnion("");
18+
>fnUnion("") : any
19+
>fnUnion : { a: string; } | ((a: string) => void)
20+
>"" : ""
21+
22+
declare const fnUnion2: (<T extends number>(a: T) => void) | (<T>(a: string) => void)
23+
>fnUnion2 : (<T extends number>(a: T) => void) | (<T>(a: string) => void)
24+
>a : T
25+
>a : string
26+
27+
fnUnion2("");
28+
>fnUnion2("") : any
29+
>fnUnion2 : (<T extends number>(a: T) => void) | (<T>(a: string) => void)
30+
>"" : ""
31+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
declare const union: { a: string } | { b: string }
2+
union("");
3+
4+
declare const fnUnion: { a: string } | ((a: string) => void)
5+
fnUnion("");
6+
7+
declare const fnUnion2: (<T extends number>(a: T) => void) | (<T>(a: string) => void)
8+
fnUnion2("");

0 commit comments

Comments
 (0)