diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index cd90880654c..c2317f0d8fb 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -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); } diff --git a/tests/baselines/reference/betterErrorForUnionCall.errors.txt b/tests/baselines/reference/betterErrorForUnionCall.errors.txt new file mode 100644 index 00000000000..e9220dd99ca --- /dev/null +++ b/tests/baselines/reference/betterErrorForUnionCall.errors.txt @@ -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 '((a: T) => void) | ((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: ((a: T) => void) | ((a: string) => void) + fnUnion2(""); + ~~~~~~~~~~~~ +!!! error TS2349: This expression is not callable. +!!! error TS2349: Each member of the union type '((a: T) => void) | ((a: string) => void)' has signatures, but none of those signatures are compatible with each other. + \ No newline at end of file diff --git a/tests/baselines/reference/betterErrorForUnionCall.js b/tests/baselines/reference/betterErrorForUnionCall.js new file mode 100644 index 00000000000..53865991136 --- /dev/null +++ b/tests/baselines/reference/betterErrorForUnionCall.js @@ -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: ((a: T) => void) | ((a: string) => void) +fnUnion2(""); + + +//// [betterErrorForUnionCall.js] +union(""); +fnUnion(""); +fnUnion2(""); diff --git a/tests/baselines/reference/betterErrorForUnionCall.symbols b/tests/baselines/reference/betterErrorForUnionCall.symbols new file mode 100644 index 00000000000..1d28d3dae13 --- /dev/null +++ b/tests/baselines/reference/betterErrorForUnionCall.symbols @@ -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: ((a: T) => void) | ((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)) + diff --git a/tests/baselines/reference/betterErrorForUnionCall.types b/tests/baselines/reference/betterErrorForUnionCall.types new file mode 100644 index 00000000000..0022cf369c4 --- /dev/null +++ b/tests/baselines/reference/betterErrorForUnionCall.types @@ -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: ((a: T) => void) | ((a: string) => void) +>fnUnion2 : ((a: T) => void) | ((a: string) => void) +>a : T +>a : string + +fnUnion2(""); +>fnUnion2("") : any +>fnUnion2 : ((a: T) => void) | ((a: string) => void) +>"" : "" + diff --git a/tests/cases/compiler/betterErrorForUnionCall.ts b/tests/cases/compiler/betterErrorForUnionCall.ts new file mode 100644 index 00000000000..6288e3a3e36 --- /dev/null +++ b/tests/cases/compiler/betterErrorForUnionCall.ts @@ -0,0 +1,8 @@ +declare const union: { a: string } | { b: string } +union(""); + +declare const fnUnion: { a: string } | ((a: string) => void) +fnUnion(""); + +declare const fnUnion2: ((a: T) => void) | ((a: string) => void) +fnUnion2("");