TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

No error report on duplicated function/class declaration

Open xryeisme opened this issue 3 years ago β€’ 5 comments

Bug Report

πŸ”Ž Search Terms

πŸ•— Version & Regression Information

⏯ Playground Link

https://www.typescriptlang.org/play

πŸ’» Code

declare function foo() : any

declare class foo {
    test() : any
}

πŸ™ Actual behavior

no error report

πŸ™‚ Expected behavior

I think it should report error because we have duplicated 'foo' defintion.

xryeisme avatar Oct 18 '22 09:10 xryeisme

https://www.typescriptlang.org/docs/handbook/declaration-merging.html

β€œit’s a feature, not a bug” πŸ˜‰

fatcerberus avatar Oct 18 '22 13:10 fatcerberus

This is the syntax you would use to describe an object which is callable both with and without new, which is a thing that people do in JS sometimes.

RyanCavanaugh avatar Oct 18 '22 16:10 RyanCavanaugh

which is a thing that people do in JS sometimes.

@RyanCavanaugh Your comment is roughly 2 weeks to early.

MartinJohns avatar Oct 18 '22 16:10 MartinJohns

this is confused, because it report errors if without 'declare' keyword:

function foo() : any {}

// Class declaration cannot implement overload list for 'foo'.(2813)
class foo {
  test() : any {}
}

so can I just simpliy assume that the declaration with 'declare' is like something in header file for js but not for ts?

xryeisme avatar Oct 19 '22 01:10 xryeisme

the declaration with 'declare' is like something in header file for js but not for ts?

Yes. It basically tells TypeScript that this will exist at runtime.

MartinJohns avatar Oct 19 '22 06:10 MartinJohns

clear now, close this issue, thanks everyone for clarification

xryeisme avatar Oct 21 '22 09:10 xryeisme