Bru*_*res 30 javascript typescript
我开始使用TypeScript,现在我在5分钟的指南中关注TypeScript.当我将鼠标悬停在greeter函数名称上时,我在Visual Studio Code中收到一个奇怪的警告,如下图所示.警报是:
[ts]重复的功能实现.
function greeter(person:Person):string(+1 overload)
但是我的单个文件中没有这个独特功能的其他实现!当我运行tsc greeter.ts所有工作正常并生成js文件.
完整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));
Run Code Online (Sandbox Code Playgroud)
我为什么收到此警报?怎么解决?我看了一下这个问题,但我相信它没有关系.
小智 18
当我们打开 file.ts 和转译的 file.js 文件并执行 TSC 时,会发生此错误。
请关闭已转译的 file.js,然后重试。
小智 8
如果您在同一目录中同时拥有 src 文件(typescript)和转译文件(javascript),并在 VS Code 中打开 javascript 文件,那么您将收到错误消息。将转换后的文件输出到目录中,不会出错。使用 --outDir 标志:
tsc --outDir ./dist greeter.ts
在 VS Code 1.26.1 版本中遇到了这个问题。生成 tsconfig.json 文件并没有使我的错误消失。
小智 7
根据这篇文章,在 Typescript 文件的顶部添加这一行简单的命令: export { };
[index.ts] 导出 { };
declare const signalR: any;
declare const moment: any;
Run Code Online (Sandbox Code Playgroud)
小智 7
这可能是因为您没有tsconfig.jsonTypeScript 项目的文件。尝试创建一个tsconfig文件并写入默认的“compilerOptions”。这对我有用。tsconfig.json我使用的带有默认代码的文件是:
{
"compilerOptions": {
"module": "commonjs"
},
"exclude": [
"node_modules"
]
}
Run Code Online (Sandbox Code Playgroud)
有关 VS TypeScript 编译的更多信息,请参阅 https://code.visualstudio.com/docs/typescript/typescript-compiling
就我而言,似乎如果文件没有导入/导出任何内容,则它不会被视为模块,并且 VS Code 会假设所有文件都会连接在一起。您可以通过添加导入/导出来修复它。
我还继续将 tsconfig 设置为包含,isolatedModules以便获得更有用的错误消息。
{
"isolatedModules": true,
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8555 次 |
| 最近记录: |