57

I'm getting started with TypeScript and at moment I'm following the TypeScript in 5 minutes guide. I'm receiving a strange warning in Visual Studio Code when I hover the mouse over the greeter function name, as shown in the below image. The alert is:

[ts] Duplicate function implementation.

function greeter(person: Person): string (+1 overload)

Duplicate function implementation warning.

But there is no other implementation of this unique function in my single file! When I run tsc greeter.ts all works fine and the js file is generated.

The complete greeter.ts file:

interface Person {
    firstName: string;
    lastName: string;
}

function greeter(person: Person) {
    return "Hello, " + person.firstName + " " + person.lastName;
}

var user = { firstName: "Jane", lastName: "User" };

console.log(greeter(user));

Why am I receiving this alert? How to solve it? I took a look in this question, but I believe it isn't related.

Andrew Li
  • 55,805
  • 14
  • 125
  • 143
Bruno Peres
  • 15,845
  • 5
  • 53
  • 89

7 Answers7

114

Looks like this is a bug in Visual Studio Code. There are a few issues on GitHub about this, such as here and here. The comments on the issues imply that it was an issue, then was fixed, and has just become an issue again in v1.12.1.

It looks as if the solution is to run tsc --init to initialize the tsconfig.json in the folder.

Andrew Li
  • 55,805
  • 14
  • 125
  • 143
  • tsc --init error TS5023: Unknown compiler option 'init'. – Tyguy7 Feb 22 '19 at 03:50
  • @tyguy7 When I came across this is was because the most recent version of TSC was not the one being found on my path. Instead a very much earlier version was being found which did not support this flag. – Rory Becker Apr 30 '19 at 11:19
  • Hi @Andrew Li, I have faced a similar issue on visual studio 2019. can you please help me on this? – Mohanraj Periyannan Dec 16 '20 at 13:38
  • @MohanrajPeriyannan its all about the compiled files( *.js ) one. once you have instanciated the tsconfig.json file, if you compile, by default it compile at same directory but add "use strict"; if for some reason doesn't happens, then put manually them in another directory and from that point on that will be used or check other answers – Carmine Tambascia Mar 04 '22 at 09:48
17

When we open both file.ts and the transpiled file.js files and do TSC, this error occurs.
Please close the transpiled file.js and try again.

James Harrington
  • 3,138
  • 30
  • 32
VKV
  • 191
  • 1
  • 2
  • 4
    Why is your answer downvoted. Your reply is correct probably maybe due to how you explained it. If the .ts (typescript) and the transpiled file .js both opened in vscode you get the warning. If you closed the .js file and only .ts file (typescript) you get no error. – Micheal N.D Jul 04 '20 at 10:58
  • Yep, having the JS open was the issue for me – vandijkstef May 04 '22 at 08:47
12

If you have both the src file (typescript) and the transpiled file (javascript) in the same directory and open up the javascript file in VS Code then you'll get the error. Output the transpiled file into a directory and it will not error. Use the --outDir flag: tsc --outDir ./dist greeter.ts

Got this problem in version 1.26.1 of VS Code. Generating the tsconfig.json file did not make the error go away for me.

Andrew Li
  • 55,805
  • 14
  • 125
  • 143
Eric Pitcher
  • 271
  • 3
  • 5
  • 1
    use tsc --outDir ./dist *.ts --watch if you want the file to continue to transpile as you are writing. – Stimsoni Feb 14 '21 at 06:04
7

According to this article, add this simple line in the top of your Typescript file export { };

[index.ts] export { };

declare const signalR: any;
declare const moment: any;
Nidust
  • 599
  • 1
  • 8
  • 16
7

This might be because you don't have a tsconfig.json file for your TypeScript project. Try creating a tsconfig file and write a default "compilerOptions". It worked for me. The tsconfig.json file with default code that I used is :

{
    "compilerOptions": {
        "module": "commonjs"
    },
    "exclude": [
        "node_modules"
    ]
}

For more info on VS TypeScript Compiling please refer https://code.visualstudio.com/docs/typescript/typescript-compiling

arunkc
  • 71
  • 1
  • 2
5

In my case, it seems that if the file didn't import/export anything it wasn't considered a module and VS Code would assume that all the files would get concatenated together. You can fix it by adding an import/export.

I also went ahead and set my tsconfig to include isolatedModules so that I would get a more helpful error message.

{
    "isolatedModules": true,
}
Kevin Raoofi
  • 1,023
  • 11
  • 16
2

Old post but hope this helps someone:

I encountered this problem when I had my code written like this:

function myFunc() { ... }
module.exports = {
    myFunc
}

I fixed the problem by writing it this way instead:

export function myFunc() { ... }