【问题标题】:Cannot re declare block scoped variable error when I am not re-declaring it anywhere in my file当我不在文件中的任何位置重新声明时,无法重新声明块范围变量错误
【发布时间】:2018-04-13 06:04:53
【问题描述】:

我在一个名为 package-list.ts 的文件中收到一条错误消息,上面写着 error TS2451: Cannot redeclare block-scoped variable 'packageList',该文件包含以下代码:

const packageList = {
    'FREE': 'free',
    'GOLD': 'gold',
    'SILVER': 'silver',
    'BRONZE': 'bronze',
    'STARTER': 'starter'
}

module.exports = packageList;

使用tsc 编译器选项:

    {
    "compilerOptions": {
        "target": "es2015",
        "outDir": "../dist",
        "allowJs": true,
        "module": "commonjs",
        "noEmitOnError": false,
        "noImplicitAny": false,
        "strictNullChecks": true
    },
    "exclude": [
        "node_modules",
        "**/*.spec.ts"
    ]
}

我没有在上述代码的任何地方重新声明packageList。那么这个错误的原因可能是什么?

【问题讨论】:

    标签: node.js typescript ecmascript-6


    【解决方案1】:

    这是因为packageList 正被其他模块使用。您可以尝试以下选项:

    解决方案 1

    将名称 packageList 更改为 myPackageList

    解决方案 2。

    不包括 DOM 类型。为此,请在您的 tsconfig.json 中添加一个不包含 dom 的显式 lib 属性:

    {
        "compilerOptions": {
            "lib": [
                "es2015"
            ]
        }
    }
    

    解决方案 3。

    尝试在顶部添加export {};

    【讨论】:

      猜你喜欢
      • 2023-04-08
      • 1970-01-01
      • 2018-08-29
      • 2018-08-02
      • 2020-06-07
      • 2017-04-15
      • 2016-06-15
      • 2022-06-30
      • 2018-04-15
      相关资源
      最近更新 更多