Added tests for new error messages on calls to unions.

This commit is contained in:
Titian Cernicova-Dragomir 2019-06-22 03:11:14 +03:00
parent 768318b30c
commit bc07eec015
6 changed files with 112 additions and 1 deletions

View File

@ -22090,7 +22090,7 @@ namespace ts {
);
}
function invocationError(node: Node, apparentType: Type, kind: SignatureKind, relatedInformation?: DiagnosticRelatedInformation) {
const diagnostic: Diagnostic = createDiagnosticForNodeFromMessageChain(node, invocationErrorDetails(apparentType, kind));
const diagnostic = createDiagnosticForNodeFromMessageChain(node, invocationErrorDetails(apparentType, kind));
diagnostics.add(diagnostic);
invocationErrorRecovery(apparentType, kind, relatedInformation ? addRelatedInfo(diagnostic, relatedInformation) : diagnostic);
}

View File

@ -0,0 +1,29 @@
tests/cases/compiler/betterErrorForUnionCall.ts(2,1): error TS2349: This expression is not callable.
No constituent of type '{ a: string; } | { b: string; }' is callable.
tests/cases/compiler/betterErrorForUnionCall.ts(5,1): error TS2349: This expression is not callable.
Not all constituents of type '{ a: string; } | ((a: string) => void)' are callable.
Type '{ a: string; }' has no call signatures.
tests/cases/compiler/betterErrorForUnionCall.ts(8,1): error TS2349: This expression is not callable.
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.
==== tests/cases/compiler/betterErrorForUnionCall.ts (3 errors) ====
declare const union: { a: string } | { b: string }
union("");
~~~~~~~~~
!!! error TS2349: This expression is not callable.
!!! error TS2349: No constituent of type '{ a: string; } | { b: string; }' is callable.
declare const fnUnion: { a: string } | ((a: string) => void)
fnUnion("");
~~~~~~~~~~~
!!! error TS2349: This expression is not callable.
!!! error TS2349: Not all constituents of type '{ a: string; } | ((a: string) => void)' are callable.
!!! error TS2349: Type '{ a: string; }' has no call signatures.
declare const fnUnion2: (<T extends number>(a: T) => void) | (<T>(a: string) => void)
fnUnion2("");
~~~~~~~~~~~~
!!! error TS2349: This expression is not callable.
!!! 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.

View File

@ -0,0 +1,15 @@
//// [betterErrorForUnionCall.ts]
declare const union: { a: string } | { b: string }
union("");
declare const fnUnion: { a: string } | ((a: string) => void)
fnUnion("");
declare const fnUnion2: (<T extends number>(a: T) => void) | (<T>(a: string) => void)
fnUnion2("");
//// [betterErrorForUnionCall.js]
union("");
fnUnion("");
fnUnion2("");

View File

@ -0,0 +1,28 @@
=== tests/cases/compiler/betterErrorForUnionCall.ts ===
declare const union: { a: string } | { b: string }
>union : Symbol(union, Decl(betterErrorForUnionCall.ts, 0, 13))
>a : Symbol(a, Decl(betterErrorForUnionCall.ts, 0, 22))
>b : Symbol(b, Decl(betterErrorForUnionCall.ts, 0, 38))
union("");
>union : Symbol(union, Decl(betterErrorForUnionCall.ts, 0, 13))
declare const fnUnion: { a: string } | ((a: string) => void)
>fnUnion : Symbol(fnUnion, Decl(betterErrorForUnionCall.ts, 3, 13))
>a : Symbol(a, Decl(betterErrorForUnionCall.ts, 3, 24))
>a : Symbol(a, Decl(betterErrorForUnionCall.ts, 3, 41))
fnUnion("");
>fnUnion : Symbol(fnUnion, Decl(betterErrorForUnionCall.ts, 3, 13))
declare const fnUnion2: (<T extends number>(a: T) => void) | (<T>(a: string) => void)
>fnUnion2 : Symbol(fnUnion2, Decl(betterErrorForUnionCall.ts, 6, 13))
>T : Symbol(T, Decl(betterErrorForUnionCall.ts, 6, 26))
>a : Symbol(a, Decl(betterErrorForUnionCall.ts, 6, 44))
>T : Symbol(T, Decl(betterErrorForUnionCall.ts, 6, 26))
>T : Symbol(T, Decl(betterErrorForUnionCall.ts, 6, 63))
>a : Symbol(a, Decl(betterErrorForUnionCall.ts, 6, 66))
fnUnion2("");
>fnUnion2 : Symbol(fnUnion2, Decl(betterErrorForUnionCall.ts, 6, 13))

View File

@ -0,0 +1,31 @@
=== tests/cases/compiler/betterErrorForUnionCall.ts ===
declare const union: { a: string } | { b: string }
>union : { a: string; } | { b: string; }
>a : string
>b : string
union("");
>union("") : any
>union : { a: string; } | { b: string; }
>"" : ""
declare const fnUnion: { a: string } | ((a: string) => void)
>fnUnion : { a: string; } | ((a: string) => void)
>a : string
>a : string
fnUnion("");
>fnUnion("") : any
>fnUnion : { a: string; } | ((a: string) => void)
>"" : ""
declare const fnUnion2: (<T extends number>(a: T) => void) | (<T>(a: string) => void)
>fnUnion2 : (<T extends number>(a: T) => void) | (<T>(a: string) => void)
>a : T
>a : string
fnUnion2("");
>fnUnion2("") : any
>fnUnion2 : (<T extends number>(a: T) => void) | (<T>(a: string) => void)
>"" : ""

View File

@ -0,0 +1,8 @@
declare const union: { a: string } | { b: string }
union("");
declare const fnUnion: { a: string } | ((a: string) => void)
fnUnion("");
declare const fnUnion2: (<T extends number>(a: T) => void) | (<T>(a: string) => void)
fnUnion2("");