首页 > 解决方案 > 出现错误“TS2451:无法重新声明块范围变量‘全局’。” 使用打字稿和 webpack?

问题描述

我正在尝试使用 Typescript 和 Webpack 为 Google Apps Script 建立一个项目。

我遇到了一个我不明白的奇怪错误:

TS2451:无法重新声明块范围变量“全局”

global用于gas-webpack-plugin在 Google Apps 脚本中创建可访问的函数。

我在 webpack ( index.ts) 的入口点中使用它:

declare let global: any;

global.banana = () => {
  // Method used in Google Apps Script
};

我收到此错误,但我不确定我在哪里重新声明global. 我尝试排除node_modulestsconfig.json

{
  "compilerOptions": {
    "sourceMap": true,
    "target": "es5",
    "module": "es2015"
  },
  "exclude": [
    "node_modules"
  ],
}

如果我在声明行之前添加导入,错误就会消失。导入的内容似乎并不重要。这是我添加的。import { Whatever } from './whatever';

whatever.ts内容:

export class Whatever {
  static whatever(): string {
    return "Nothing makes sense";
  }
}

显然我有一个解决方法,只是想知道为什么脚本会因错误而停止?

标签: typescriptwebpackgoogle-apps-script

解决方案


推荐阅读