0

I am trying to set up a project for Google Apps Script with Typescript and Webpack.

I run into an odd error that I don't understand:

TS2451: Cannot redeclare block-scoped variable 'global'

global is used by the gas-webpack-plugin to create accessible functions in Google Apps Script.

I use it here in my entry point for webpack (index.ts):

declare let global: any;

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

I get this error, but I'm not sure where I am redeclaring global. I tried excluding node_modules from tsconfig.json:

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

If I add an import before the declare line the error goes away. The content of import doesn't seem to matter. This is what I added. import { Whatever } from './whatever';

whatever.ts contents:

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

Clearly I have a workaround, just wondering why script stops with the error?

Kos
  • 4,890
  • 9
  • 38
  • 42
K Egan
  • 1
  • You need to understand the difference between modules and scripts. In a module, all declarations are local to the module and thus do not conflict with any declarations elsewhere. If you want to modify a global variable named global, you can't do it the way your are attempting. – Aluan Haddad Feb 03 '20 at 16:39
  • 1
    Just doing a little googling based on your comment. Am I right to say that by adding the import statement I am changing changing the index.ts to a module which changes the variable scope? Thanks. – K Egan Feb 03 '20 at 16:54
  • 1
    Yes. You may find this useful https://stackoverflow.com/a/49899979/1915893 – Aluan Haddad Feb 03 '20 at 17:44

0 Answers0