stephenh / ts-proto

An idiomatic protobuf generator for TypeScript
Apache License 2.0
2.16k stars 347 forks source link

TS2304: Cannot find name 'global' #70

Closed SazhinDima closed 4 years ago

SazhinDima commented 4 years ago

I generated typescript file and try to import it. But i have error "TS2304: Cannot find name 'global'" on line throw new global.Error("Value is larger than Number.MAX_SAFE_INTEGER");

Help me please.

stephenh commented 4 years ago

@SazhinDima are you trying to run the ts-proto output in Node on the backend or in a browser on the frontend?

stephenh commented 4 years ago

@SazhinDima I'm guessing that you're trying to run in the browser, where globalThis needs to be used instead of global. Can you try version 1.21.2? If you're still having issues, let me know more details about what environment you're trying to run in and what your tsconfig looks like.

SazhinDima commented 4 years ago

@stephenh i'm runing in the Node.js (react project)

tscinfig:

{ "compilerOptions": { "module": "commonjs", "moduleResolution": "node", "sourceMap": true, "jsx": "react", "target": "es2018", "baseUrl": ".", "experimentalDecorators": true, "skipLibCheck": true, "typeRoots": [ "./node_modules/@types" ] }, "include": [ "src/*/", "globalCss.d.ts" ], "exclude": [ "node_modules", "dist" ] }

SazhinDima commented 4 years ago

If i put in top: declare var global: any;

evethig works correct. But i don't know is it ok.

stephenh commented 4 years ago

You probably need to do npm install @types/node because global comes from node. This is not a ts-proto issue but just a TypeScript/node issue, so please use stackoverflow/etc. to resolve from here on out.

JeViCo commented 1 year ago

If someone still have this issue, try using globalThis instead

stephenh commented 1 year ago

Hi @JeViCo , thanks for the note; per globalThis, yes, at this point we have a pretty elaborate function to get the right global object:

const tsProtoGlobalThis: any = (() => {
  if (typeof globalThis !== "undefined") {
    return globalThis;
  }
  if (typeof self !== "undefined") {
    return self;
  }
  if (typeof window !== "undefined") {
    return window;
  }
  if (typeof global !== "undefined") {
    return global;
  }
  throw "Unable to locate global object";
})();

And then we do throw new tsProtoGlobalThis.Errror(...)