Skip to content

[ts] Duplicate function implementation #24925

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
dpindusa opened this issue Jun 13, 2018 · 8 comments
Closed

[ts] Duplicate function implementation #24925

dpindusa opened this issue Jun 13, 2018 · 8 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@dpindusa
Copy link

Steps to Reproduce:

  1. Create 1.ts file (Code as mentioned below)
  2. Create 2.ts file (Code as mentioned below)
  3. Create tsconfig.json using tsc -init. (This step is not mandatory)
  4. Observer red underline under the function name

VS Code Version: 1.24.0
TypeScript Version: 2.3.4
OS Version: Windows 8.1 Enterprise

Search Terms: TypeScript Visual Studio Code [ts] Duplicate function implementation

Code

//1.ts File
function test(){
    console.log("File 1 Error");
}

//2.ts File
function test(){
    console.log("File 2 Error");
}

Expected behavior:

Actual behavior:

Related Issues: #10804
file1
file2
error

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Jun 13, 2018
@RyanCavanaugh
Copy link
Member

These functions are in the global scope and only one of them can exist at a time.

@brikou
Copy link

brikou commented Aug 21, 2018

In order to prevent functions to be in global scope, you can add export {}; on top (or just export this function):

// 1.ts
export {};

function test(){
    console.log("File 1 Error");
}
// 2.ts
export {};

function test(){
    console.log("File 2 Error");
}

see https://stackoverflow.com/questions/40900791/cannot-redeclare-block-scoped-variable-in-unrelated-files

@xgqfrms
Copy link

xgqfrms commented Mar 10, 2020

@brikou Thanks a lot!

  1. global scope
  2. export {}; ES module, local module scope

@kyoonart
Copy link

good

@MuhammadAbdullah54321
Copy link

This gives error in the console that
"Uncaught ReferenceError: exports is not defined"

@davschne
Copy link

Well, the suggested solution works if I'm using ES modules, but what about when I'm dealing with CommonJS modules? The same error occurs, but there doesn't appear to be a way to fix it. If I add export {}; I get SyntaxError: Unexpected token 'export' when trying to run the generated JS in Node. My sense is that when using "module": "commonjs" in tsconfig.json, such checks should be disabled. @RyanCavanaugh, thoughts?

@andrewbranch
Copy link
Member

export {} is downleveled in "module": "commonjs": https://www.typescriptlang.org/play?module=1#code/KYDwDg9gTgLgBAbwL4G4BQQ

If you’re writing .js files that you’re not going to compile, you can get the same effect of modulification with module.exports = {}.

@davschne
Copy link

I guess I did something wrong before, because it's working now. Thanks, @andrewbranch!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

8 participants