Typescript: [ts] Duplicate function implementation

Created on 13 Jun 2018  路  4Comments  路  Source: microsoft/TypeScript


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

Working as Intended

Most helpful comment

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

All 4 comments

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

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

@brikou Thanks a lot!

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

good

Was this page helpful?
0 / 5 - 0 ratings