-
Notifications
You must be signed in to change notification settings - Fork 349
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
env=browser treated same as env=both. Causes globalThis.Buffer typescript error #967
env=browser treated same as env=both. Causes globalThis.Buffer typescript error #967
Comments
The generated bytesFromBase64 and base64FromBytes functions now only include code that's required for the specified env: For env=node, the functions now exclusively use globalThis.Buffer to de-/encode to and from JSON For env=browser, globalThis.btoa/atob is used For env=both, the two functions use either the node or browser implementations depending on whether globalThis.Buffer exists
The generated bytesFromBase64 and base64FromBytes functions now only include code that's required for the specified env: For env=node, the functions now exclusively use globalThis.Buffer to de-/encode to and from JSON For env=browser, globalThis.btoa/atob is used For env=both, the two functions use either the node or browser implementations depending on whether globalThis.Buffer exists
The generated bytesFromBase64 and base64FromBytes functions now only include code that's required for the specified env: For env=node, the functions now exclusively use globalThis.Buffer to de-/encode to and from JSON For env=browser, globalThis.btoa/atob is used For env=both, the two functions use either the node or browser implementations depending on whether globalThis.Buffer exists
The generated bytesFromBase64 and base64FromBytes functions now only include code that's required for the specified env: For env=node, the functions now exclusively use globalThis.Buffer to de-/encode to and from JSON For env=browser, globalThis.btoa/atob is used For env=both, the two functions use either the node or browser implementations depending on whether globalThis.Buffer exists
For me ts-proto generates code like this: if (globalThis.Buffer) Which leads to typescript error:
I want to support both node and browser with the same code. What would be the correct setting? |
The generated bytesFromBase64 and base64FromBytes functions now only include code that's required for the specified env: For env=node, the functions now exclusively use globalThis.Buffer to de-/encode to and from JSON For env=browser, globalThis.btoa/atob is used For env=both, the two functions use either the node or browser implementations depending on whether globalThis.Buffer exists
The generated bytesFromBase64 and base64FromBytes functions now only include code that's required for the specified env: For env=node, the functions now exclusively use globalThis.Buffer to de-/encode to and from JSON For env=browser, globalThis.btoa/atob is used For env=both, the two functions use either the node or browser implementations depending on whether globalThis.Buffer exists
@jonaskello I guess for If you'd like to submit a PR to do that, that'd be great! I'm going to close this issue since #999 fixes the behavior with env=browser. |
Got it. If I can find the time I'll make a PR for that. I was able to work around it for now. We have all the generated files in a separate package with it's own tsconfig so I could disable the strict any setting for those files without affecting the rest of the app. |
@jonaskello it was an easy fix, so I went ahead and pushed out a change to add the |
When outputting with
env=browser
you get the same output asenv=both
.This is generally OK, except when you have strict typescript checks on. You end up in a situation where the generated code includes a line like:
if (globalThis.Buffer)
to see if Buffer exists. This makes sense forenv=both
, but will cause typescript errors on a browser environment because globalThis type does not includeBuffer
.As a workaround, we can enable: both of:
env=browser,globalThisPolyfill=true
.However, it seems to me that a cleaner approach might be to have the
env
enum disable checks forglobalThis.Buffer
altogether. In searching through today's ts-proto code, I cannot see any cases where theenv
is checked against anything exceptnode
. Thereby accidentally makingboth
equivalent tobrowser
, which is probably not the original intent.Some proposals:
env=browser
will stop checking for globalThis.Bufferenv=browser
is detected, then we auto-enable the polyfill.The text was updated successfully, but these errors were encountered: