Throw errors or show warnings when duplicate function name


#1

If you have two functions that have the same name there is no error.

Expected
Error or warning if you have the same function defined more than once.

Actual
There’s no indication that duplicate function exists.


#2

Sorry, but I have to disagree with you once again :wink:

This is the way JavaScript behaves (and I like XD for not trying to reimplement JS in their APIs). Since in Javascript, function x() {} is the same as var x = function() {}, it is also possible (without any problems) to “reassign” functions. Therefore, it is even expected that

function a() {/* Do something here */}

function a() { /* Do something else */}

throws no errors or warnings since it’s just the same as writing

var a;
a = function() {/* Do something here */}

a = function() { /* Do something else */}

Therefore, I disagree with the request since implementing it would mean going away from “normal” JavaScript or EcmaScript…


#3

Here’s also a screenshot to “proove” that this is standard JS behavior (from the Chrome developer console, which – while not perfect – represents a relatively “clean” implementation of JS):

(as you can see, there are no warnings or errors as it’s perfectly valid – i.e. correct in syntax and semantics – JavaScript)


#4

It does throw errors when you declare variables of the same name twice:

Example:

let abc = 10;
let abc = 20;

Results in TS error:

Cannot redeclare block-scoped variable ‘abc’.ts(2451)
let abc: number

Runtime:

Export error:SyntaxError: Identifier ‘abc’ has already been declared


#5

And that’s also expected: After you’ve declared a variable name (by using let), you can reassign it (by using abc = 20;). You – however – can’t declare it again. JavaScript, however, doesn’t treat named functions as “declaring” (that’s one of those things you either love or hate about JS :wink:) , but as assigning and only declaring if necessary (like shown above)…


#6

As @pklaschka said, this is just how the language spec requires all JavaScript engines to behave. However, linter tools are great for catching these kinds of issues. ESLint and JSHint are two of the most popular.