TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

Incorrect diagnostic message in TS2814

Open bvanjoi opened this issue 4 months ago • 1 comments

🔎 Search Terms

2814, function body

🕗 Version & Regression Information

  • regression at v4.4.4.

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=4.4.4#code/MYGwhgzhAEBiD29oG9oChqegIzAJwEYAKAShWgF80q0AzAVwDtgAXAS3kbkVIG40A9AIB+APWgBRAEpSA8lIBccJqw5cA7mxYALHPAAmbAKYxgYLpxABPaAFsjeAOZHomndFCQIJ6DrAtofBcwW2xjRhYAOiA

💻 Code

// tests/cases/compiler/callOverloads1.ts

class Foo { 
    bar1() { }
}

function Foo();
//~^ error report: Function with bodies can only merge with classes that are ambient.

🙁 Actual behavior

The function Foo actually has no body (it's just a declaration), yet the error message says "Function with bodies...", which may be a bit confusing.

🙂 Expected behavior

Either show a hint like "Only functions can merge with ambient classes," or suppress the error report for functions without a body because class Foo will report Class declaration cannot implement overload list for 'Foo'.

Additional information about the issue

No response

bvanjoi avatar Jul 07 '26 07:07 bvanjoi

FWIW function Foo(); is not valid syntax. You need the declare keyword and (with noImplictAny, a return type). Fixing this does not affect the other errors, though.

snarbles2 avatar Jul 07 '26 12:07 snarbles2

Hi, I've noticed that the diagnostic message for function declarations is misleading when there is no body. I plan to adjust the logic in checker.ts to check for function bodies and update the message in diagnosticMessages.json accordingly. Could you please assign this issue to me? Thank you!

Mahnoor-Zaffar avatar Jul 12 '26 15:07 Mahnoor-Zaffar