【问题标题】:Issues with TypeScript after compilation to JavaScript - "Duplicate identifier error"编译为 JavaScript 后 TypeScript 出现问题 - “重复标识符错误”
【发布时间】:2021-01-09 16:26:55
【问题描述】:

在 VSCode 中,我使用 TypeScript 并在编译为 JavaScript 时遇到问题,IDE 会抱怨某些元素(类、变量等)是重复的。这是因为它们存在于 TypeScript 文件和作为结果创建的 JavaScript 文件中。

我想这是一个相当普遍的问题,但要澄清几件事:

  1. 这似乎不会阻止 TypeScript 编译
  2. 仅当 .js 文件在 IDE (VSCode) 中打开时才会发生这种情况

下面是显示我的意思的图像:

我在使用 npm 或命令行方面不是很有经验,所以我不确定我是否无意中启用了这种行为或遗漏了一些东西——但这显然是不可取的,所以我想知道是否有人能想到什么可能导致问题?首先,这更可能是 VSCode 问题还是 TypeScript 问题?

我查看了一些关于 SO 的类似问题 - 例如this,但似乎没有人遇到与我完全相同的问题。

非常感谢!

【问题讨论】:

    标签: javascript node.js typescript visual-studio npm


    【解决方案1】:

    我相信您以某种方式在同一个文件夹中同时拥有 typescript 和 javascript。你能分享你的 tsconfig 文件吗?需要注意的重要一点是outdir,它将确保编译后的文件不会保存在同一个目的地。

    {
      "compilerOptions": {
    
        "target": "es6",                          /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
        "module": "commonjs",                     /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
        "sourceMap": true,                     /* Generates corresponding '.map' file. */
        "outDir": "dist",                        /* Redirect output structure to the directory. */
    
        "strict": true,                           /* Enable all strict type-checking options. */
    
    
        "moduleResolution": "node",            /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
        "esModuleInterop": true,                  /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
    
    
    
        "skipLibCheck": true,                     /* Skip type checking of declaration files. */
        "forceConsistentCasingInFileNames": true  /* Disallow inconsistently-cased references to the same file. */
      }
    }
    

    Package.json,这将允许您运行 npm 命令,例如 npm start....

    {
      "name": "learning_ts",
      "version": "1.0.0",
      "description": "",
      "main": "dist/target_group.js",
      "scripts": {
        "debug": "tsc && node --nolazy dist/target_group.js",
        "start": "tsc && node dist/target_group.js",
        "test": "echo \"Error: no test specified\" && exit 1"
      },
      "author": "",
      "license": "ISC",
      "dependencies": {
        "@types/node": "^14.11.1",
        "aws-sdk": "^2.756.0",
        "dotenv": "^8.2.0"
      },
      "devDependencies": {
        "tslint": "^5.12.1",
        "typescript": "^3.3.3"
      }
    }
    
    

    【讨论】:

    • 嗨 Shubham,有趣的是,我的根文件夹中似乎没有 tsconfig 文件 - 这可能吗?我在哪里可以找到它?
    • 您可能需要创建它。我将编辑我的答案并复制 package.json 和 tsconfig。根据您的需要进行更改。
    • 如果您看到之前的消息,请忽略它 - 我以为我已经解决了问题,但问题仍然存在。这是我的 package.json 文件:{ "name": "ts-sandbox", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "comp": "tsc --outDir js sandbox.ts" }, "keywords": [], "author": "", "license": "ISC", "dependencies": { "eslint": "^7.9.0", "typescript": "^4.0.3" } }
    • 创建一个 tsconfig 文件似乎已经修复了它,是的。谢谢!
    猜你喜欢
    • 2020-07-23
    • 2016-10-29
    • 2016-11-04
    • 1970-01-01
    • 1970-01-01
    • 2021-05-21
    • 2019-02-18
    • 1970-01-01
    • 2013-10-05
    相关资源
    最近更新 更多