typescript 打字稿指南给出“重复函数实现”警告

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44192541/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 04:34:53  来源:igfitidea点击:

Typescript guide gives "Duplicate function implementation" warning

javascripttypescript

提问by Bruno Peres

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

我正在开始使用 TypeScript,目前我正在5 分钟内遵循TypeScript指南。当我将鼠标悬停在greeter函数名称上时,我在 Visual Studio Code 中收到一个奇怪的警告,如下图所示。警报是:

[ts] Duplicate function implementation.

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

[ts] 重复函数实现。

函数迎宾(人:人):字符串(+1重载)

Duplicate function implementation warning.

重复函数实现警告。

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

但是在我的单个文件中没有这个独特功能的其他实现!当我运行时tsc greeter.ts一切正常,并且生成了 js 文件。

The complete greeter.tsfile:

完整的greeter.ts文件:

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.

为什么我会收到此警报?如何解决?我看了一下这个问题,但我相信它不相关。

回答by Andrew Li

Looks like this is a bug in Visual Studio Code. There are a few issues on GitHub about this, such as hereand 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.

看起来这是 Visual Studio Code 中的一个错误。GitHub 上有一些关于此的问题,例如这里这里。对问题的评论暗示这是一个问题,然后被修复,并且刚刚在 v1.12.1 中再次成为一个问题。

It looks as if the solution is to run tsc --initto initialize the tsconfig.jsonin the folder.

看起来解决方案是运行tsc --init以初始化tsconfig.json文件夹中的 。

回答by Nidust

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

根据这篇文章,在你的 Typescript 文件export { };的顶部添加这个简单的行

[index.ts] export { };

[index.ts] 出口{};

declare const signalR: any;
declare const moment: any;

回答by Arun K C

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

这可能是因为您没有tsconfig.json用于 TypeScript 项目的文件。尝试创建一个tsconfig文件并编写一个默认的"compilerOptions"。它对我有用。tsconfig.json我使用的带有默认代码的文件是:

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

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

有关 VS TypeScript 编译的更多信息,请参阅 https://code.visualstudio.com/docs/typescript/typescript-compiling

回答by Eric Pitcher

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

如果您在同一目录中同时拥有 src 文件(typescript)和转换后的文件(javascript),并在 VS Code 中打开 javascript 文件,那么您将收到错误消息。将转译后的文件输出到目录中,不会出错。使用 --outDir 标志: 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.

在 VS Code 1.26.1 版本中遇到了这个问题。生成 tsconfig.json 文件并没有使我的错误消失。

回答by VKV

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.

当我们同时打开 file.ts 和转译的 file.js 文件并执行 TSC 时,会出现此错误。
请关闭转换后的 file.js 并重试。